Color Code a Date Field Based on Expiry Date

Community Alums
Not applicable

Hello,

I have a date field called Expires (u_expires) on the cmdb_ci_business_app (Business Application) table. I want to achieve the following:

  • 15 days before the expiration date: The u_expires field should be highlighted in amber (yellow).
  • On the day of expiration: The u_expires field should be highlighted in red.

I'm looking for a solution where this happens automatically without manually loading the form (i.e., it should be a background process). How can I implement this in ServiceNow?

 

I tried using a Scheduled Script Execution to calculate the expiration and update a flag, but I'd prefer to avoid creating a new field just for this purpose. How can I achieve this using Business Rules, Scheduled Jobs, or other recommended approaches in ServiceNow?

Any guidance or suggestions would be appreciated!

1 REPLY 1

Akash4
Kilo Sage
Kilo Sage

Hi Sam,

You can try using this sample onLoad Client Script & modify accordingly:

function onLoad() {
var expireDate = g_form.getValue('u_expires');
var today = new GlideDate();
var diffDays = gs.dateDiff(today.getDisplayValue(), expireDate, true);

if (diffDays <= 15 && diffDays > 0) {
g_form.setFieldStyle('u_expires', 'background-color: yellow; color: black;');
} else if (diffDays <= 0) {
g_form.setFieldStyle('u_expires', 'background-color: red; color: white;');
} else {
g_form.setFieldStyle('u_expires', 'background-color: white; color: black;');
}
}

 

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.