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

Clara Lemos
Mega Sage
Mega Sage

Hi @phaug ,

 

One thing that you might want to add to your script is a current.setAbortAction(true); after your error message, so that the user is not able to do to the modification.

 

But what exactly is not working as you expected? Is your field type integer?

 

If that helps please mark my answer as correct / helpful!
And if further help is needed please let me know

Cheers

Thanks, I did add that to the script.

 

I'd expect to see an error if I increased the number in the field by over 100. But, I'm still able to change the number from the list view, increasing the field by over 100 without the error message appearing. And yes, the field type is an integer.

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);

 

 

Thanks, I tried that and it doesn't appear to be working. I'm not seeing anything in the logs. Could it be the condition I have listed above the script?

 

Condition: current.u_sequence_priority.changes()

 

phaug_0-1699307048572.png

 

phaug_1-1699307059743.png