Report: script in filter condition

smarino
Kilo Contributor

Hi All,

I'm trying to create a report and I want to add a dynamic filter condition (probably with a script?) to include only records with field "Year" set to the next year.

For example, if I run the report now (2014), it should show only records with field "Year"=2015.

Any help is appreciated.

Thanks in advance.

5 REPLIES 5

jfarrer
Mega Guru

Is the Year part of a date field or just a text field? If it's part of a date then you can use the Relative operator in the condition builder:


relativecondition.jpg


If it's a text field then you should be able to put in the javascript directly with a condition of:


"Year" "is" "javascript:gs.yearsAgo(-1)"



If you need some additional logic or depending on the version your instance is on, there could be some additional steps to set up a dynamic filter. In Calgary and Earlier you can do it with a Script Include that defines a function (with the same name as the script include) that returns the value that you need. You then call it with "javascript:yourCustomFunctionName()". In Dublin they've added some additional security layers so you'll need to follow the instruction on the wiki here: Using Filters and Breadcrumbs - ServiceNow Wiki


smarino
Kilo Contributor

Hi James,


thanks for your response, I need to retrieve next year (2015) not previous (2013).


As the "Year" field is a String type, I tried with the following script:



javascript:gs.beginningOfNextYear().substring(0,3)



Unfortunately the output of the report shows also records relative to following years (2016, 2017, ...)



Many thanks.


You'll want to make sure that you're using the comparison operator of "is" instead of the default of "starts with".



Using gs.yearsAgo with a negative value will get dates in the future. So using gs.yearsAgo(-1) will yield the same year as gs.beginningOfNextYear().



To use substring to get the full year you'll need to do:


javascript:gs.beginningOfNextYear().substring(0,4)



In doing a little testing using 3 for the last parameter for substring returns 201 not 2015.


smarino
Kilo Contributor

Can't use "is" condition..."Year" field is a choice, with "is" condition SNOW allows only to choose between the listed values.