Splunk Search

How to store a single value search result in some variable in a Splunk HTML page to display in a javascript alert popup?

harshal_chakran
Builder

Hi,

I have a dashboard in html code with one search query which provides the result in Single Numeric Value.

Is there any way that I can take the result of my search in some variable?
I want to display it in a JavaScript alert popup i.e. alert("my search result is: " + search_result_var).
Which variable in html code of dashboard stores the search result.
Please Help..!!!

0 Karma

gun1989
New Member

Hi i am new to splunk, can you please give me a sample code which i can use?

0 Karma

Flynt
Splunk Employee
Splunk Employee

One way to achieve this is put a listener on your search and then assign the value to a token. For instance if my single panel was using search1 and the token I wanted to set was "user".

 var tokens = mvc.Components.getInstance("default");
 var singleresult=splunkjs.mvc.Components.get("search1");
     singleresult.data("results").on("data", function(results) { 
             var username = results._data['rows'][0][0];
             tokens.set("user",username);
      });

You could then use that token or variable anywhere in your HTML. You can use it in a search as any other token by calling it with $user$ or in your script by the variable alert("my search result is: " + username). If you're not using the alert inside the listener, you can retrieve the token value and set it as a variable anywhere else by -

 var desiredvariable=tokens.get("user");

djfang
Explorer

@Flynt[Splunk]

Thanks for your help! 🙂

0 Karma

Raghav2384
Motivator

Have you tried assigning the value to a input token and call it ? might work. I had a same single value panel...i created a token and assigned the single value and then called the token like

value is : $single_value$

and it displays just fine.

Hope this helps!
Thanks,
Raghav

0 Karma

naveen069
New Member

Thank you so much!! It was really helpful 🙂

0 Karma
Get Updates on the Splunk Community!

Developer Spotlight with William Searle

The Splunk Guy: A Developer’s Path from Web to Cloud William is a Splunk Professional Services Consultant with ...

Major Splunk Upgrade – Prepare your Environment for Splunk 10 Now!

Attention App Developers: Test Your Apps with the Splunk 10.0 Beta and Ensure Compatibility Before the ...

Stay Connected: Your Guide to June Tech Talks, Office Hours, and Webinars!

What are Community Office Hours?Community Office Hours is an interactive 60-minute Zoom series where ...