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

kaushal_snow
Mega Sage

Hi @Abdul ,

 

Check below code 

 

(function executeRule(current, previous) {
current.u_compliant = '';
var closedVal = current.getValue('closed_at');
var dueVal = current.getValue('due_date');
if (closedVal && dueVal) {
var gdtClosed = new GlideDateTime(closedVal);
var gdtDue = new GlideDateTime(dueVal);
var closedTs = gdtClosed.getNumericValue();
var dueTs = gdtDue.getNumericValue();
current.setValue('u_compliant', (closedTs <= dueTs) ? 'Yes' : 'No');
}
})(current, previous);

 

Please make sure the Business Rule will run for before and Insert & Update checked. Set Condition to trigger when closed_at or due_date changes (or when state becomes Closed).

 

Try this once and let me know. If you find this helpful, please accept this as a solution and hit the helpful button..

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

The value is not getting populated in the field

The problem is most likely in when/how the Business Rule runs rather than the code itself. Try checking business rule table, when condition, insert ans update, trigger condition etc..

 

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Chaitanya ILCR
Kilo Patron

Hi @Abdul ,

try this

 

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

    var closed_date_str = current.getValue('closed_at');
    var due_date_str = current.getValue('due_date');
    if (closed_date_str && due_date_str) {
        GlideDateTime.subtract(current.closed_at.getGlideObject(), current.due_date.getGlideObject()) > 0 ? current.setValue('u_compliant', "Yes") : current.setValue('u_compliant', "No");
    }

})(current, previous);

 if not working please share more details

is your BR a before update BR? if not make i before update if it's after use current.update() else make it before update

 

 

 

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

Regards,
Chaitanya