How to prevent Business Rule loop?

peterraeves
Mega Guru

We have created a situation where sometimes we need to keep two records in sync. So we created a Business Rule that triggers when the one record triggers, updating the other and a second Business Rule doing the reverse. You can already feel where I'm heading with this right? When record A is updated, the BR triggers, updates record B and that triggers a BR that updates A again. ServiceNow notices the loop and quits there, which is nice, but it still has one update too many.

This could easily be avoided to compare the old and new values and if they are the same, the update should not happen. But this is not as easy with journal fields... The comments and work notes fields need to be synchronised as well.

I tried comparing current.comments.getJournalEntry(1) with other.comments.getJournalEntry(1), but this is not always the same, as it might take more than a second to process, changing the timestamp. I tried comparing the content of the entry, without the timestamp and user, but that gives a problem when the user types in 2 times the exact same thing in the comments or work notes. So is there another way, to let servicenow know that they can run all other business rules against the update of the second record, but not the same one again?

14 REPLIES 14

This wouldn't work. I tried that on my first try and it failed, because there was a second difference between the posting of both notes, failing the timestamp check.



Edit: My final solution would indeed be to check whether both had been posted within a time range of 2 seconds. I'd have to retrieve the time stamp either from the journal entries table, or subtract it from the journal entry and compare contents, but that would require me to do additional checks on every field. I hoped for a simpler solution, where I could check on a higher level.


I think it should work. There should not be any time stamp issue. For example when you put work_notes in a problem record, the work_notes including the time stamp from problem will be copied too. Then the after business rule on problem_task will come into play and the if block evaluates to false.



See the below snapshot which shows the time stamp from problem was copied to the problem_task


find_real_file.png


I tried this and it doesn't work. At first, you type 856 in the worknotes, so in Problem you will have 08-07-2016 08:56:28, but then if you are unlucky, in Problem Task you will have 08-07-2016 08:56:29, if the second occurs at the second switch. So when you check the Problem again, it will not have a match, and add the work notes again.



Edit: Oké, after thinking about it some more, I understand what you mean. Yes, your trick would work, but then the worknotes in the Problem Task are cluttered with double timestamp, so that's not what I want either. I only want to copy the content of the worknotes.


HugoFirst
Kilo Sage

Did you try setworkflow(false)?   I think if you use that when you first update the record being synched, no BR's will run and you shouldn't have the loop.



See Add custom business rules to SLAs   for a description about how to use this feature.


This is the simplest answer, but you may interfere with other downstream BRs on the receiving table that still DO need to happen.




-Brian