How to add a literal static date value in a date interactive filter?

Todd O
Tera Guru

I'm wanting to add a literal static date value as a selectable option for an interactive date filter. To do this, there is a business rule called "Get Date Filter Options for Date Filters" where you are supposed to just add your entry as another line in there.  I can add simple ones using relative date references and those are easy, and they work great. But, I'm wanting to add a static date value (e.g., '2022-01-01') and none of the variations I've tried are working. Below are just a few of the basic ones I've tried. 

 

answer.add('180_Base year@javascript:new GlideDateTime("'+new RequestUtils().getBeginningDate()+'")@javascript:new GlideDateTime("'+new RequestUtils().getEndingDate()+'")', gs.getMessage('Base year'));

answer.add("180_Base year@javascript:gs.dateGenerate('2021-01-01','00:00:00')@javascript:gs.dateGenerate('2022-12-31','23:59:59')", gs.getMessage('Base year'));

 

There is a KB on this topic at this location. https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0749822

In this KB it is stated that relative dates can be used and they provide many examples. They also state that literal static values can be used but, unfortunately no examples are provided. Here is a snippet of that paragraph from that KB.

ToddO_0-1687901298958.png

 

Below are additional community posts that I have already tried and none of them provide an answer nor are marked as answered. Hopefully this avoids someone replying with one of these links saying this may help…(unless of course there is a discussion that I missed and provides an answer).

 

    1. https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0749822
    2. https://www.servicenow.com/community/platform-analytics-forum/what-is-the-syntax-to-add-custom-date-...
    3. https://www.servicenow.com/community/developer-forum/script-include-for-date-filter/m-p/1592995
    4. https://www.servicenow.com/community/developer-forum/add-year-choices-to-quot-get-date-filter-option...
 

Additionally, I’ve added the “Debug homepage filters” onto my dashboard to be able to see the filter being applied. I’ve noticed that for all the date filters applied, it always uses the “ON” filter and it appears that is hard coded. I would like a “BETWEEN” version for my use case. Is there anyway to make this a “BETWEEN?” No matter what I put in the “Get Date Filter Options for Date Filters” business rule, this filter is always “ON” value. See image below.

ToddO_1-1687903745276.png

 

 Any help is greatly appreciated!
1 ACCEPTED SOLUTION

Todd O
Tera Guru

I now have the solution because I opened a support case for this. The response from support was an example entry to put in that mentioned business rule. I'm hoping this will help others as well. I was not able to find an example like this anywhere.  Here is the sample that I have verified works. Also, the technician said she's going to try and get the KB updated so that an example is there too.

 

answer.add('FULL_Year 2023@01/01/2023 00:00:00@12/31/2023 23:59:59', gs.getMessage('Full Year 2023'));

View solution in original post

7 REPLIES 7

Todd O
Tera Guru

Here is some additional information. I was able to get the below example working using gs.daysAgoStart/End and then calling a script include to get a dynamic value for the days to pass into the function. It works! But, there is still a problem. If I change the script include (RequestUtils) at all (or the date rolls over to next day and the value generated by the script include is different), the system will automatically remove this custom filter entry from the Interactive filter definition.  I then have to go into the Interactive Filter definition and add it back into the list of "Selected" items.

 

It seems I cannot find a way to have the new custom date filter to be dynamic (e.g., having the number of days change in the script include). Any suggestions are appreciated!

Todd

 

  1. answer.add('180_Base year@javascript:gs.daysAgoStart(' + new x_custom_appid.RequestUtils().getDays() + ')@javascript:gs.daysAgoEnd(0)', gs.getMessage('Base year'));

 

ToddO_0-1687975859617.png

 

Just a note on my message above. I think SN purposefully did it this way in that you have to re-add the entries after the BR is updated.  I just have to remember to update the interactive filter if I make changes to the BR. 

Todd O
Tera Guru

I now have the solution because I opened a support case for this. The response from support was an example entry to put in that mentioned business rule. I'm hoping this will help others as well. I was not able to find an example like this anywhere.  Here is the sample that I have verified works. Also, the technician said she's going to try and get the KB updated so that an example is there too.

 

answer.add('FULL_Year 2023@01/01/2023 00:00:00@12/31/2023 23:59:59', gs.getMessage('Full Year 2023'));

Jiri K
Tera Contributor

Hello @Todd O 

Thank you so much for your post and the example your provided!  I am in the same situation as you, where I just want to filter by static date values e.g. for the month of January 2023, February 2023 etc as individual filters.  

I used your example code line (with some tiny word changes):

Screenshot 2023-12-04 at 17.16.44.png

answer.add('205_Year2023@01/01/2023 00:00:00@12/31/2023 23:59:59', gs.getMessage('Year 2023'));


However, for some reason the filter doesn't do anything when selected, please see attached code snippet above. 

Can you (or someone else) please see anything which doesn't look right?
@Todd O Would you be so kind as to share a screenshot of your working code in-situ as way for me to compare? 

Any assistance with this would be greatly appreciated!