change color on due date field when approaching due date and expired

Nic Omaha
Tera Guru

Sorry I know this is probably a simple question but how do I get the Due Date field on Request, and Item in Service Catalog to turn yellow if its say 2 days before due date and red if its on or past the due date?

 

req due date field.JPG

item due date field.JPG

13 REPLIES 13

User140988
Kilo Guru

Hi Nicholas,



You can do this by using field styles. You can set up a field style on the field, choose a background color and a simple javascript condition comparing the due date to current date will help.


Thanks! Im just getting my feet wet with Java scripting so that's my one struggle!


Hi Nicholas,



The wiki link Defining Field Styles - ServiceNow Wiki provides an example for the overdue style, here it is modified for the due_date field:



javascript: (gs.dateDiff(gs.now(), current.due_date.getDisplayValue(), true) * 1) < 0



Here is the reference for dateDiff: GlideSystem Date and Time Functions - ServiceNow Wiki



But basically, the dateDiff is getting the difference in seconds between Now (gs.now) and the current record's due date field.   If that value is less than 0 (due date is before now) then the style is applied to that record.   The * 1 simply converts the string to a number.



So with a little modifying, we can change this script to create another field style which applies when the difference ranges from 172800 seconds (2 days) to 0 seconds before the due date.



javascript: (gs.dateDiff(gs.now(), current.due_date.getDisplayValue(), true) * 1) < 172800 && (gs.dateDiff(gs.now(), current.due_date.getDisplayValue(), true) * 1) >= 0



The key is to get the javascript to return 'true' under the conditions in which the style should apply and false otherwise.   The first script will return true when Now - Due Date is less than 0 seconds.   The second script will return true when Now - Due Date is less than 172800 seconds AND Now - Due Date is greater than or equal to 0 seconds are both true.


This made perfect sense and worked like a charm!!!!! thank you !