below before business rule is not working

madhusagar
Tera Contributor

Hi All,

 

The query is, I need to restrict to add affected CIs to the change request if the change state is not in implement/review/cancelled/closed.

 

I have written the below script but the script is not working, the script is not entering into second if loop.

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

var taskRef = current.task.getRefRecord();
var tableName = taskRef.getTableName();
var state = taskRef.state;
gs.info('Table Name: ' + '-->' + tableName);

if (tableName === 'change_request' || tableName === 'change_task') {
if (state === 500 || state === 600 || state === 900 || state === 1000) {
gs.info("Enter into second IF loop");
gs.addErrorMessage('The Change request is in ' + taskRef.state + " " + ', So cannot add affected CI');
current.setAbortAction(true);
}
return;
}

})(current, previous);

 

Thanks & Regards,

1 ACCEPTED SOLUTION

@madhusagar 

Glad to know.

Please close the thread by marking appropriate response as correct so that it benefits future readers.

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@madhusagar 

script looks fine.

Did you check the state value is correct in comparison?

What's your debugging analysis?

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

I check the choices in sys_choice table, and choices are looked good.

@madhusagar

BR is on which table and what type?

it should be on task_ci table and before insert

Did it go inside the 2nd IF?

what came in the table name and state value?

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

Ankur Bawiskar
Tera Patron
Tera Patron

@madhusagar 

update as this, the choice values for states you are using are wrong

 

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

    var taskRef = current.task.getRefRecord();
    var tableName = taskRef.getTableName();
    var state = taskRef.state;
    gs.info('Table Name: ' + '-->' + tableName);

    // stateIN - 1, 0, 4

    if (tableName == 'change_request' && (state == -1 || state == 0 || state == 4)) {
        gs.info("Enter into second IF loop");
        gs.addErrorMessage('The Change request is in ' + taskRef.state + " " + ', So cannot add affected CI');
        current.setAbortAction(true);
    }

})(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