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.

Business rule if statement not working as expected

phaug
Tera Contributor

I need to create a business rule to limit how high a user can change a integer value from the list view of a table. I had to convert this from a client script because I found out the users were changing directly from the list instead of the form itself. For some reason it's not showing the error as I'd expect.


I have the rule set to run Before an Update is made with these conditions.

 

Condition: current.u_sequence_priority.changes()

 

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

    // Add your code here
   
    if (current.u_sequence_priority > previous.u_sequence_priority + 100) {
        gs.addErrorMessage("Cannot increment the priority sequence by more than 100.");
    }
   
})(current, previous);
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Add another log before the if to see if the script is getting triggered.  If not then try moving the Condition to Filter Conditions, or adding it to the if statement.  As written, the second part of your if statement is likely 500100 if the previous value was 500.  You can log these too so you can see where it's going wrong. Also incorporating Clara's excellent suggestion to prevent the update when the error message is displayed, it would look something like this:

 

(function executeRule(current, previous /*null when async*/ ) {
    gs.addInfoMessage ('BR running'); //temporary logging 
    var cursp = current.u_sequence_priority;
    var presp = previous.u_sequence_priority;
    gs.addInfoMessage('cur: ' + cursp + ' prev: ' + presp);
    if (parseInt(cursp) > parseInt(presp) + 100) {
        gs.addErrorMessage("Cannot increment the priority sequence by more than 100.");
        current.setAbortAction(true);
    }
})(current, previous);

 

 

View solution in original post

7 REPLIES 7

@phaug Since you have put the condition current.u_sequence_priority.changes() the BR script execution will only trigger if the value in u_sequence_priority field changes.

 

Hope this helps.

Yes, that's why I said to take it out if the script is still not getting triggered.  Try it without, then try adding it to the Filter Conditions or an if condition in the script.  Double-check that this custom field name is exactly correct (space- and case-sensitive) and that it exists on the sn_customerservice_task table.

Thanks, yeah it was a user error. I just caught that I spelled the variable name backwards. Once I removed the condition and updated the name it sends the error message as expected and changes the number back.