Dashboards & Visualizations

How can I reference a search job in JavaScript?

christoffertoft
Communicator

Im doing some custom javascript magic in a dashboard were I have a need for re-running a search

<search id="rerun_me_after_button_is_clicked">
<query> ... </query>
</search>

How can I rerun this in my javascript through referring to the id? (or some other identifier)?

my idea is something like:

var sm = new SearchManager({
  id: "rerun_me_after_button_is_clicked",
  earliest_time: "-24h@h",
  latest_time: "now",
  preview: true,
  cache: false
  }

sm.StartSearch()

Not sure if this is how it works though?

Labels (1)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @christoffertoft,

Can you please try by adding below line to execute search ?

mvc.Components.get("rerun_me_after_button_is_clicked").startSearch();

Your javascript would be like this

    require([
        'underscore',
        'jquery',
        'splunkjs/mvc',     
        'splunkjs/mvc/simplexml/ready!'], function (_, $, mvc) {
        ( 'splunkjs/mvc/simplexml/ready!',  )

        .
        .
        .
        .
mvc.Components.get("rerun_me_after_button_is_clicked").startSearch();
    .
        .
        .
        .

});

Thanks

View solution in original post

493669
Super Champion

Try this:

require([
      'underscore',
      'jquery',
      'splunkjs/mvc',
      'splunkjs/mvc/tableview',
      'splunkjs/mvc/simplexml/ready!'
  ], function(_, $, mvc, TableView) {

  $(document).ready(function () {
         $("#mybutton").on("click", function (e) {
            var mySearch = mvc.Components.get("rerun_me_after_button_is_clicked");
            mySearch.startSearch();
         });
     });

  });

christoffertoft
Communicator

Thanks, that did the trick! I can't award you both with the answer unfortunately, but you get a point at least! 🙂

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @christoffertoft,

Can you please try by adding below line to execute search ?

mvc.Components.get("rerun_me_after_button_is_clicked").startSearch();

Your javascript would be like this

    require([
        'underscore',
        'jquery',
        'splunkjs/mvc',     
        'splunkjs/mvc/simplexml/ready!'], function (_, $, mvc) {
        ( 'splunkjs/mvc/simplexml/ready!',  )

        .
        .
        .
        .
mvc.Components.get("rerun_me_after_button_is_clicked").startSearch();
    .
        .
        .
        .

});

Thanks

sandyBDF
Engager

that don't work for me i have a buton like this (in the code source) :

id="button_update" class="btn" data-set-token="run_check" data-value="true" style="background-color:#68E674;"

and the js : equire([
'underscore',
'jquery',
'splunkjs/mvc',
'splunkjs/mvc/tableview',
'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc, TableView) {

$(document).ready(function () {
$("button_update").on("click", function (e) {
var mySearch = mvc.Components.get("toto");
mySearch.startSearch();
});
});

});
But the search never start any idea ?

0 Karma

richgalloway
SplunkTrust
SplunkTrust

If the accepted answer doesn't work for you then you should post a new question describing your particular situation.

---
If this reply helps you, Karma would be appreciated.
0 Karma

christoffertoft
Communicator

Thank you! That helped 🙂

0 Karma
Get Updates on the Splunk Community!

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...