Dashboards & Visualizations

How to Use Javascript Variable in Search Manager query?

splunknoob_rip
Loves-to-Learn Everything

Hello Everyone, 

 

First off, thanks in advance to everyone who takes the time to contribute to this post!

 

I've got custom html code in simple xml and was able to grab data from a textpart and parse it into a  JavaScript variable captured using the code below. I'm trying to use the variable captured in the search query in the SearchManager function. So far I've only been able to set static values such as eval test = "Working" but have had no luck passing in a JavaScript variable. 

 

 

 

require([
    "underscore",
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc/simplexml/ready!",
], function(_, mvc, SearchManager) {
var mysearch = new SearchManager({
        id: "mysearch",
        autostart: "false",
        search: '| makeresults | eval test = captured | collect index = "test_index"'
    });
$("#btn-submit").on("click", function () {
        // Capture value of the Text Area
        var captured = $("textarea#outcome").val();
        mysearch.startSearch();
});
});
});

 

 

 

 

Labels (2)
Tags (1)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@splunknoob_rip 

Can you please try this?

require([
    'jquery',
    "underscore",
    'splunkjs/mvc',
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc/simplexml/ready!"
], function ($, _, mvc, SearchManager) {
    var submittedTokens = mvc.Components.get('submitted');
    console.log("Hiee 1");
    // var defaultTokens = mvc.Components.get("default");

    var mysearch = new SearchManager({
        id: "mysearch",
        autostart: "false",
        search: '| makeresults | eval test = "$capturedValue$" | collect index = "test_index"',
        preview: false,
    }, { tokens: true, tokenNamespace: "submitted" });

    $("#btn-submit").on("click", function () {
        // Capture value of the Text Area
        var captured = $("textarea#outcome").val();
        tokens.set("capturedValue", captured);
        submittedTokens.set(tokens.toJSON());
        mysearch.startSearch();
    });
});

 

I hope this will help you.

Thanks
KV
If any of my replies help you to solve the problem Or gain knowledge, an upvote would be appreciated.

0 Karma
Get Updates on the Splunk Community!

Splunk Observability Synthetic Monitoring - Resolved Incident on Detector Alerts

We’ve discovered a bug that affected the auto-clear of Synthetic Detectors in the Splunk Synthetic Monitoring ...

Video | Tom’s Smartness Journey Continues

Remember Splunk Community member Tom Kopchak? If you caught the first episode of our Smartness interview ...

3-2-1 Go! How Fast Can You Debug Microservices with Observability Cloud?

3-2-1 Go! How Fast Can You Debug Microservices with Observability Cloud? Learn how unique features like ...