Correct my code

Abdul
Tera Contributor

Hi 

 

I have a requirement where in i have to create a field named compliant in problem task and that field should be read only (this is done) and that should display Yes if the task is closed date is less than due date, and No the other way round.

 

I tried to achieve this via BR, and i am not getting the proper results.

Can you please correct me on what i did wrong.

 

Thanks 

 

Code:

 

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

    // Add your code here
    current.u_compliant = '';
    var closed_date_str = current.getValue('closed_at');
    var due_date_str = current.getValue('due_date');
    if (closed_date_str && due_date_str) {
        // Convert to Date objects
        var closed_date = new Date(closed_date_str);
        var due_date = new Date(due_date_str);
    }
    if (due_date > closed_date) {
        current.setValue('u_compliant', "Yes");
    } else {
        current.setValue('u_compliant', "No");
    }


})(current, previous);
11 REPLIES 11

this code is not populating anything in my field

Ankur Bawiskar
Tera Patron
Tera Patron

@Abdul 

Considering your business rule is before Insert/update and the field name and choice value Yes, No are correct

This code should work

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

    // Add your code here
    var closed_date_str = new GlideDateTime(current.getValue('closed_at'));
    var due_date_str = new GlideDateTime(current.getValue('due_date'));
    if (due_date.getNumericValue() > closed_date_str.getNumericValue()) {
        current.setValue('u_compliant', "Yes");
    } else {
        current.setValue('u_compliant', "No");
    }

})(current, previous);

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

i tried the negative scenario to reflect the compliant as No still i am getting as Yes, even though i set the due date as past value and i close it now

@Ankur Bawiskar please help

@Abdul 

so what debugging did you do?

what logs you added and what are your findings?

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