After BR causing duplication of activity

Mickey_Cegon
Tera Expert

I created an After BR to calculate the calendar_duration on the rm_story table. I need to calculate the diff between the opened_at and closed_at fields, so I set it to run After Active changesTo False. Works great, but it shows up in the activity log once, with a second set of activity showing and a duplicate entry in the Audit History list.

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

I tried it as a Before BR, but it didn't work because the closed_at isn't populated yet.

Any suggestions would be helpful. Thanks in advance!

Mickey Cegon

Farm Bureau Financial Services, Inc.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

If you run this as an AFTER BR, it is going to trigger all the BRs again with the current.update().



If closed_at is not populated like you say, try doing a current.setWorkflow(false) right before and give your BR a very high order (10,000) so it runs last to avoid stopping other BRs that might run after it.



Reference:


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


View solution in original post

7 REPLIES 7

Chuck Tomasi
Tera Patron

If you run this as an AFTER BR, it is going to trigger all the BRs again with the current.update().



If closed_at is not populated like you say, try doing a current.setWorkflow(false) right before and give your BR a very high order (10,000) so it runs last to avoid stopping other BRs that might run after it.



Reference:


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


Thanks for the quick response, Chuck. I took a look at the order of all the BR running, and it ends up that the Set Closure Fields BR was set to an order of 10,000. So, I changed my BR to a Before, changed the Order to 20,000, and removed the current.update() from the rule. It now only updates the one time, so I think I've got it fixed. Once I set it to wait until the closure fields were actually set, then the calculation worked.




Mickey Cegon


Ding ding ding! Thanks for the update Mickey. Nobody ever pays attention to the order field until things get weird.



If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.



If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View.



Thank you


Sagar V1
Mega Expert

Hi Cegon,



Avoid using current.update() in a business rule script. The update() method triggers business rules to run on the same table for insert and update operations, leading to a business rule calling itself over and over. Changes made in before business rules are automatically saved when all before business rules are complete, and after business rules are best used for updating related, not current, objects. When a recursive business rule is detected, the system stops it and logs the error in the system log. However, current.update() causes system performance issues and is never necessary.


You can prevent recursive business rules by using the setWorkflow() method with the false parameter. The combination of the update() and setWorkflow()methods is only recommended in special circumstances where the normal before and after guidelines mentioned above do not meet your requirements.



Thanks,


Sagar