change color on due date field when approaching due date and expired
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2014 08:35 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2014 08:39 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2014 08:50 AM
Thanks! Im just getting my feet wet with Java scripting so that's my one struggle!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2014 10:20 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2014 07:29 AM
This made perfect sense and worked like a charm!!!!! thank you !