Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Runjay Patel
Giga Sage

Hi @madhusagar ,

 

Try below code.

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

    var taskRef = current.task.getRefRecord();
    var tableName = taskRef.getTableName();
    var state = taskRef.getValue('state'); 
   
    if (tableName === 'change_request') {
        if (state == -1|| state == 0 || state == 3 || state == 4) {
            gs.info("Entered into second IF loop");
            gs.addErrorMessage('The Change request is in state ' + taskRef.state + ', so you cannot add an affected CI.');
            current.setAbortAction(true); // Prevent further processing
        }
    }

})(current, previous);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

In this series i have explained about ServiceNow Discovery right from scratch like how you can enable the discovery plugin, what are the discovery prerequisites, what all question you should ask for your client before start the discovery implementation, how you can configure discovery schedules ...

Dominik9
Tera Guru

Hi @madhusagar 

 

The state variable may is not an numeric value as you check it in the second if statement. And as you use "===" it also checks for the type. You may stringify the value? 

Something like:

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

var taskRef = current.task.getRefRecord();
var tableName = taskRef.getTableName();
var state = taskRef.getValue("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 ' + state  + " " + ', So cannot add affected CI');
current.setAbortAction(true);
}
return;
}

})(current, previous);

 

Best regards
Dominik

madhusagar
Tera Contributor

Thanks Everyone i fix it

@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