How can I add another option (next 2 hours for example) in the dropdown list placed after the filter operator for date/time fields?

kiriljosifovski
Kilo Contributor

Hi,

I am trying to add another custom option in the dropdown list (next 2 hours for example) which is placed after the filter operator.

The filter should look something like:

Ordered after next 2 hours

As I tried to find how to change, I began to get the impression that this list is pregenerated and not editable.


Thanks in advance,

Kiril.

11 REPLIES 11

John Rausch
Tera Contributor

I ended up needing to modify some default JS values that SN setup.



function getDaysAgoByDay( strDay )


{


  var weekdays = [ "Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat" ];


  var date = new Date();


  var index = weekdays.indexOf(strDay);


  var difference =   date.getDay() - index;


  if (difference < 0 )


  {


  difference = 7 + difference;


  }


  return   difference;


}




calNS.protoVal = [


'Today: [0d     ]0d   ]0d   ',


'Yesterday: [1d     ]1d   ]1d   ',


'Tomorrow: [-1d   ]-1d ]-1d ',


'This week: [=w     ]=w   ]=w   ',


'Last week: [<w     ]<w   ]<w   ',


'Next week: [>w     ]>w   ]>w   ',


'This month: [=m     ]=m   ]=m   ',


'Last month: [<m     ]<m   ]<m   ',


'Next month: [>m     ]>m   ]>m   ',


'Last 3 months: [3m     ]=m   [3m   ',


'Last 6 months: [6m     ]=m   [6m   ',


'Last 9 months: [9m     ]=m   [9m   ',


'Last 12 months: [12m   ]=m   [12m ',


'This quarter: [=q     ]=q   ]=q   ',


'Last quarter: [1q     ]1q   ]1q   ',


'Last 2 quarters: [1q     ]=q   ]2q   ',


'Next quarter: [-1q   ]-1q ]-1q ',


'Next 2 quarters: [-1q   ]-2q ]-2q ',


'This year: [=y     ]=y   ]=y   ',


'Next year: [>y     ]>y   ]>y   ',


'Last year: [<y     ]<y   ]<y   ',


'Last 2 years: [<y     ]=y   [<y   ',


'Last Sunday: ['+getDaysAgoByDay( "Sun" )+'d     ]0d   ['+getDaysAgoByDay( "Sun" )+'d   ',


'Last Monday: ['+getDaysAgoByDay( "Mon" )+'d     ]0d   ['+getDaysAgoByDay( "Mon" )+'d   ',


'Last Tuesday: ['+getDaysAgoByDay( "Tue" )+'d     ]0d   ['+getDaysAgoByDay( "Tue" )+'d   ',


'Last Wednesday: ['+getDaysAgoByDay( "Wed" )+'d     ]0d   ['+getDaysAgoByDay( "Wed" )+'d   ',


'Last Thursday: ['+getDaysAgoByDay( "Thurs" )+'d     ]0d   ['+getDaysAgoByDay( "Thurs" )+'d   ',


'Last Friday: ['+getDaysAgoByDay( "Fri" )+'d     ]0d   ['+getDaysAgoByDay( "Fri" )+'d   ',


'Last Saturday: ['+getDaysAgoByDay( "Sat" )+'d     ]0d   ['+getDaysAgoByDay( "Sat" )+'d   ',


'Last 7 days: [7d     ]0d   [7d   ',


'Last 30 days: [30d   ]0d   [30d ',


'Last 60 days: [60d   ]0d   [60d ',


'Last 90 days: [90d   ]0d   [90d ',


'Last 120 days: [120d ]0d   [120d',


'Current hour: [0h     ]0h   ]0h   ',


'Last hour: [1h     ]1h   ]1h   ',


'Last 2 hours: |2h     |0h   |2h   ',


'Current minute: [0n     ]0n   ]0n   ',


'Last minute: [1n     ]1n   [1n   ',


'Last 15 minutes: [15n   ]0n   [15n ',


'Last 30 minutes: [30n   ]0n   [30n ',


'Last 45 minutes: [45n   ]0n   [45n ',


'One year ago: |12m   ]=m   |12m '


];



sysvalues['calendar'] = calNS.compileCal();



I threw this code into a global UI script and now all my date filters have the ability to select based on some last day of the week and the current day. For example, a gauge/report where I want all of the data between last thursday and today. You can do the same for hours just add an array item like this:



'Next 2 hours [-2h     ]0h   [-2h   ',



or something like that.



EDIT/ADDITION: If you want to use these kinds of custom filters in reports or saved filters then you will need to create a scheduled job with logic to appropriately update the filter anywhere it is used. These custom filter values are best used in live filter scenarios.


Which default SN script did you modify to add your option? I looked in all script includes and UI scripts and could not find that function name.


I'm looking to add minutes to my search.


Thanks.


Do we have the UI Script Name for this one? Appreciate the help.


I had to create a new UI Script. I made sure it was global so that all the instance could use it. What the script I posted does is overwrite the default servicenow filters.



Now looking back on this you might be better off appending the new filters you want instead of recreating the whole array. This way any new filters won't need to be added to your UI Script every time they are changed.


I believe the options for dates are set in the BR "Get Date Filter Options for Date Filters".   But be very aware that if you modify this, you may have upgrade issues in the future.   If you modify this BR, you will not get any further updates to this list in the upgrade process.



I would explore other options if possible.