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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...