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.

if risk score is low then state should remain new, it shouldn't move to assess

Gulfam Shafee3
Tera Expert

I want to create a change model for low risk. If risk is not low then it should give a message that "this change model is for low risk change". and change can't progress to the approval state. 

1 ACCEPTED SOLUTION

I have already created a before update BR. Where the condition is Risk is not low and Risk is not empty. 

 

Script: 

 gs.addInfoMessage("This change model is intended for low-risk changes. Please utilize the normal model instead.");
    cancelWorkflow();

    function cancelWorkflow() {
        new WorkflowApprovalUtils().cancelAll(current, 'This change model is intended for low-risk changes. Please utilize the normal model instead.');
        var wf = new Workflow();
        wf.deleteWorkflow(current);
        current.state = '-5';
        current.update();

    }
 
It is working as expected.

View solution in original post

3 REPLIES 3

Sohail Khilji
Kilo Patron

Hi @Gulfam Shafee3 ,

 

You can create a BR:

(function executeRule(current, previous /* previous() if it is an async business rule */) {
    if (current.risk == 'Low') {
    
        gs.info('Change ' + current.number + ' is low risk. Proceeding to approval.');
    } else {

        current.state = 'Review';
        current.update();
        gs.addErrorMessage('This change model is for low risk changes only.');
    }

})(current, previous);

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi Sohail, 

 

Thank you for quick response, I have already created a before update BR. Where the condition is Risk is not low and Risk is not empty. 

 

Script: 

gs.addInfoMessage("This change model is intended for low-risk changes. Please utilize the normal model instead.");
  if(current.state == -5)
    cancelWorkflow();

    function cancelWorkflow() {
        new WorkflowApprovalUtils().cancelAll(current, 'This change model is intended for low-risk changes. Please utilize the normal model instead.');
        var wf = new Workflow();
        wf.deleteWorkflow(current);
        current.update();
    }
 
Note: Change is in new state, it should remain in new state if the risk is moderate or high. i am triggering flow in assess state, where the approval policy apply

I have already created a before update BR. Where the condition is Risk is not low and Risk is not empty. 

 

Script: 

 gs.addInfoMessage("This change model is intended for low-risk changes. Please utilize the normal model instead.");
    cancelWorkflow();

    function cancelWorkflow() {
        new WorkflowApprovalUtils().cancelAll(current, 'This change model is intended for low-risk changes. Please utilize the normal model instead.');
        var wf = new Workflow();
        wf.deleteWorkflow(current);
        current.state = '-5';
        current.update();

    }
 
It is working as expected.