Getting Data In

How to add dynamic value as default value in check box ?

snehalk
Communicator

Hello Everyone,

Can we use dynamic value as default value for check box? currently am trying below code, but unfortunately don't know how to add my dynamic value current_month_year as checked in my form

<input type="checkbox" token="currentdate">
      <label>date</label>
        <choice value="*">ALL</choice>
        <default>current_month_year</default>

     <fieldForLabel>currentdate</fieldForLabel>
      <fieldForValue>current_month_year</fieldForValue>
      <search>
      <query>| makeresults | eval current_month_year=strftime(now(),"%Y-%m") 
| table current_month_year</query>
</search>

      <valuePrefix>"</valuePrefix>
      <valueSuffix>"</valueSuffix>

      </input>

Can any one help me here

1 Solution

niketn
Legend

@snehalk, Checkbox input does not seem to accept token as default field.
Based on the example, seems like you want to use checkbox as toggle between All and Current Month. Instead of checkbox can you use radio input?

 <input type="radio" token="currentdate">
       <label>date</label>
       <choice value="*">ALL</choice>
       <default>$tokCurrentMonthYear$</default>
       <fieldForLabel>current_month_year</fieldForLabel>
       <fieldForValue>current_month_year</fieldForValue>
       <search>
           <query>| makeresults 
             | eval current_month_year=strftime(now(),"%Y-%m") 
             | table current_month_year</query>
           <progress>
                   <set token="tokCurrentMonthYear">$result.current_month_year$</set>
           </progress> 
       </search>
       <valuePrefix>"</valuePrefix>
       <valueSuffix>"</valueSuffix>
 </input>

Radio input allows passing default value as token: <default>$tokCurrentMonthYear$</default>. The same does not work with checkbox which treats it as string. This seems to be bug with Checkbox which handles multiple value. So your task would be achievable only through custom JavaScript if you have a constraint to use checkbox.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

niketn
Legend

@snehalk, Checkbox input does not seem to accept token as default field.
Based on the example, seems like you want to use checkbox as toggle between All and Current Month. Instead of checkbox can you use radio input?

 <input type="radio" token="currentdate">
       <label>date</label>
       <choice value="*">ALL</choice>
       <default>$tokCurrentMonthYear$</default>
       <fieldForLabel>current_month_year</fieldForLabel>
       <fieldForValue>current_month_year</fieldForValue>
       <search>
           <query>| makeresults 
             | eval current_month_year=strftime(now(),"%Y-%m") 
             | table current_month_year</query>
           <progress>
                   <set token="tokCurrentMonthYear">$result.current_month_year$</set>
           </progress> 
       </search>
       <valuePrefix>"</valuePrefix>
       <valueSuffix>"</valueSuffix>
 </input>

Radio input allows passing default value as token: <default>$tokCurrentMonthYear$</default>. The same does not work with checkbox which treats it as string. This seems to be bug with Checkbox which handles multiple value. So your task would be achievable only through custom JavaScript if you have a constraint to use checkbox.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

snehalk
Communicator

Awesome!! Thank you so much!! @niketnilay, its working as expected! now i can go with radio input, instated of checkbox for now.

0 Karma

snehalk
Communicator

@niketnilay, can we do the change in the order and label for radio? that is right now we are getting option like
1. All
2. 2017-07

Can we get like
1. Current Month

2. All

Here "Current Month" contain value "2017-07"?

Can you please help me here?

0 Karma

niketn
Legend

HI snehalk, Static options are displayed first followed by the results from the Dynamic Query. You can remove the static <choice> All and move the same to Dynamic option using the following query:

| makeresults 
| eval current_month_year=strftime(now(),"%Y-%m") 
| append [
           | makeresults 
           | eval current_month_year="*" ]
| table current_month_year

Another way to use query would be to use the following query,

| makeresults 
| eval current_month_year=strftime(now(),"%Y-%m") 
| appendcols [
           | makeresults 
           | eval All="*" ]
| fields - _time
| transpose column_name="label"
| reverse
| rename "row 1" as value
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

knielsen
Contributor

Hi,

This should do it:

<default>$current_month_year$</default>
0 Karma

snehalk
Communicator

Hello Knielsen,

its taking as string "$current_month_year$" its not taking value

0 Karma
Get Updates on the Splunk Community!

Application management with Targeted Application Install for Victoria Experience

  Experience a new era of flexibility in managing your Splunk Cloud Platform apps! With Targeted Application ...

Index This | What goes up and never comes down?

January 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Splunkers, Pack Your Bags: Why Cisco Live EMEA is Your Next Big Destination

The Power of Two: Splunk &#43; Cisco at "Ludicrous Scale"   You know Splunk. You know Cisco. But have you seen ...