Business rule appears to be running twice

mbarr
Kilo Explorer

I am working on a business rule (on insert or change, executed before) that determines whether an Overtime/Time Off Request can be submitted based on the pay period covered in the Request and the current day's Pay Period information. The business rule is functioning correctly, but it appears to be executing twice. I have a log of gs.addInfoMessage statements still active for debug purposes and by their display I can trace the execution through the script twice.

Has anyone had this experience and is there something I could/should do to only execute the script once?

9 REPLIES 9

Hi,


  yes,we should always use current.field.changes() along with current.field=="value" to ensure rule executes once.



For example first if we insert a request with value current.field=3(assuming numeric) ,rule will get triggered.Next lets say we or some standard business rule updates some field to   x value,even now condition gets satisfied and the rule will get triggered.so its good to check whether the field has changed.



Alternatively we can use current.field.changesTo(value) instead of   current.state.changes() && current.state == "value"




Thanks!


felipe_barbosa
ServiceNow Employee
ServiceNow Employee

Oh yes, this is a nice suggestion, thanks much JS!


vamsi
Tera Contributor

Hi,



I'm facing the same issue my BR is running multiple times which contains gs.eventQueue(), resulting multiple notifications.



my condition is like below (its from day1 we created this BR)


(current.u_planned_start_date.changes()&& !current.u_planned_start_date.nil())|| (current.u_planned_end_date.changes()&& !current.u_planned_end_date.nil()) ||(current.state.changes()&& !current.state.nil())



but still it is getting triggered multiple times, are there any other factors which are reasons for this kind of issue.



Thanks,


Vamsi


Thank you for your input Felipe.



unfortunately the changesTo() or changes() do not work for async business rules.



http://wiki.servicenow.com/index.php?title=Business_Rules#When_Business_Rules_Run


Note: Asynchronous business rules do not have access to the previous version of a record. Therefore, these GlideElement methods do not work with async rules: changes(), changesTo(), changesFrom().



My pattern for an async business rule is now



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


      // can't use changesTo() because we're an async rule              


      // http://wiki.servicenow.com/index.php?title=Business_Rules#When_Business_Rules_Run      


      if (current.async_state == 'trigger_value') {


              current.async_state = 'pending_value';


              current.update();


              // do some work here


              current.async_state = ''; // we're done


              current.update();


      }


})(current, previous);



Hope this helps any other dev facing this issue.


Update



I still had my rule triggered twice occasionally.


So I updated my async business rule as follows



Condition

current.getValue('async_state') == 'trigger_value'



Script

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


      // enter the working state


      current.async_state = 'pending_value';


      current.update();


      // do some work here


      current.async_state = ''; // we're done


      current.update();


})(current, previous);




Still wondering if I should enable glide.businessrule.async_condition_check or not:



https://docs.servicenow.com/bundle/helsinki-servicenow-platform/page/script/business-rules/task/t_Cr...  


To have the instance reevaluate the condition statement a second time before running an async business rule, add the system property glide.businessrule.async_condition_check and set the value to true.



Does anybody have experience on this property?