Check if record was inserted or updated in async business rule

Louis H
Tera Contributor

Hi there,

So in our situation, we have an external system with which we need to synchronize cases opened in ServiceNow. We have created a business rule in order to do so, in which javascript code is used to do a POST or PUT depending on whether the case was inserted or updated. Now, the business rule runs before the database operation, at which point we can check if the case is a new record with current.isNewRecord().

The problem is that sometimes, on our end, the process stalls for some reason, like waiting for the access token or some other thing, which in turn stalls in the ServiceNow instance because it is stuck waiting for the processing to be done on our end.

We would  like to switch the business rule to async so that it runs after the database processing and doesn't block ServiceNow, but we can't figure out how to verify if the operation done on the case was an insert or an update at that point in the execution.

Can someone point me towards a solution to do this in an async business rule?

Thanks in advance! 

1 ACCEPTED SOLUTION

Hmm...so the condition builder still works for async BR in terms of changes and changes to/changes from, are you able to do anything with that?

Meaning...it may sound like you need two BRs, one where condition is created changes (which would mean the record is new), then another BR with other conditions (if applicable), but not the created changes condition. So you'd use that to determine if insert or update.

The condition builder and condition field (advanced view) both support the changes(), changesTo(), and changesFrom() methods.

https://docs.servicenow.com/bundle/orlando-application-development/page/script/business-rules/refere...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

can you try to print current.operation() in the business rule; it should give whether it is insert or update

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

current.operation() does not work for async Business Rule. It will work fine for before and after Business Rule.

Allen Andreas
Administrator
Administrator

Hi,

I believe these will still work (example):

if(current.operation() == "update") {
  current.updates ++; } 
  if(current.operation() == "insert") {
    current.updates = 0; }

https://docs.servicenow.com/bundle/london-application-development/page/script/business-rules/concept...

Please mark reply as Helpful/Correct. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi thanks for the answers, I tried to log the value of current.operation() and it's null, the operation() method doesn't seem to work when the business rule is set to run async. It returns correct values if we set it to run before or after database operations though.