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

the code does not work for the else part, meaning if the record is getting closed after the due date the compliant should reflect as no that is not happening 

Nishant8
Giga Sage

Hello @Abdul, If you want to keep the value as No always and Yes when due date is greater than closed date then, could you try as below

- BR should be configured to run Before and order as 2001 (as attached below)

Nishant8_0-1754664456858.png

- in the advanced section use following script:

(function executeRule(current, previous /*null when async*/ ) {
	current.u_compliant = 'No';
    var closed_date = new GlideDateTime(current.closed_at).getNumericValue();
    var due_date = new GlideDateTime(current.due_date).getNumericValue();
    if ( due_date > 0 & closed_date > 0 & due_date > closed_date )
		current.u_compliant = 'Yes';

})(current, previous);

 

Regards,

Nishant