How to find if current date has passed the due date in ServiceNow client scrip?t

Snehal13
Kilo Sage

My requirement : I have a field on the form as 'Due Date'  and want to check if current date has past this due date or not when user opens the form

 

Accordingly,  need to show infoMessage on the form.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Snehal13 

you can use onLoad client script and compare like this

function onLoad() {
    var nowDateTime = new Date().getTime();
    var dueDate = new Date(g_form.getValue('due_date')).getTime();
    if (dueDate < nowDateTime) {
        g_form.addInfoMessage('Your message here');
    }
}

Another way is to use UI policy

AnkurBawiskar_0-1750698326867.png

 

AnkurBawiskar_1-1750698354567.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Snehal13 

you can use onLoad client script and compare like this

function onLoad() {
    var nowDateTime = new Date().getTime();
    var dueDate = new Date(g_form.getValue('due_date')).getTime();
    if (dueDate < nowDateTime) {
        g_form.addInfoMessage('Your message here');
    }
}

Another way is to use UI policy

AnkurBawiskar_0-1750698326867.png

 

AnkurBawiskar_1-1750698354567.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

One correction i missed to ask in.the original question

If current date is past the due date,  I need to update a field on the form. So can you suggest a server side solution as well

@Snehal13 

you can use a before update business rule and check this

var nowDateTime = new GlideDateTime().getNumericValue();
var dueDateTime = new GlideDateTime(current.due_date).getNumericValue();

if (dueDateTime < nowDateTime) {
    current.u_field = 'your value';
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chaitanya ILCR
Kilo Patron

Hi @Snehal13 ,

 

create a display Business rule

ChaitanyaILCR_0-1750699019440.png

with script

ChaitanyaILCR_1-1750699049281.png

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    g_scratchpad.isPastDueDate = GlideDateTime.subtract(current.due_date.getGlideObject(), new GlideDateTime()).getNumericValue(); //replace due_date with your field name

})(current, previous);

replace due_date with your field name if required

 

create a client script of your requirement onLoad or onChange
I have created a onload client script for testing (use the scratchpad object to know if the due date has passed and directly display the message

ChaitanyaILCR_2-1750699139631.png

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (g_scratchpad.isPastDueDate > 0) {
        g_form.addErrorMessage('already passed due date')
    }
}

 result

ChaitanyaILCR_3-1750699171134.png

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya