best practice for updating same record in after business rule

MaharshiC
Tera Contributor

Hi,

 

I want to update the same record in my table after it is inserted . So what is the best practice for updating same record in after insert business rule as current.update() might show recursive call in the health scan reports and without that it is not updating the record like it does for before insert br.

 

Regards,

Maharshi

1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron
Giga Patron

Hi @MaharshiC ,

 

In the rare case in which a current.update() cannot be avoided and must be used as no other method can be found to accomplish the update as needed, in order to prevent the recursion that can cause the performance issues, the current.update() should be used in conjunction with the setWorkflow(false) function.  As an example, the following code snippet shows a very basic update to a record and use of the setWorkflow() function:

current.status = 1;
current.setWorkflow(false);
current.update();
current.setWorkflow(true);

Refer below ServiceNow kb article 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0715782

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

 

View solution in original post

5 REPLIES 5

Mark Manders
Mega Patron

What is your reason for updating the ticket after insert? Why can't it be done before insert? 

Next to that: using 'current.update()' isn't best practice indeed, but if you need to use it, you need to use it. You just need to be aware that it can trigger all kinds of other logic (other rules/flows/events/etc). 

 

Your 'might show recursive call in the health scan reports' is no reason not to use it. The fact that it shows in the scan is that it isn't best practice, but sometimes it is necessary. Just comment it in the script so it is seen on the next scan why this is working like this.

 

You could also switch from the BR to a flow, since flows always run 'after', or check what happens when you make it 'async' if it doesn't have to happen immediately after insert.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

PrashantLearnIT
Giga Sage

Hi @MaharshiC 

 

To update the same record in a table after it is inserted without a recursive call, you can try these best practices:
  • Use the setWorkflow() method
    Set the parameter to false to stop business rules and other functions from running on the database access. 
     
  • Avoid using current.update()
    This method triggers business rules to run on the same table for insert and update operations, which can lead to a recursive call. 
     
  • Modify the business rule
    Make the business rule fire before insert/update and change the script so that you are not using current.update(). 
     
  • Transform the After Business Rule into a Before Business Rule
    This eliminates the need for current.update().
********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

raj chavan
Tera Guru

Hi @MaharshiC 

 

To avoid recursion in an After Insert Business Rule use events to update the record

One way - 
1. Fire an Event in the After Insert Business Rule using gs.eventQueue()
   gs.eventQueue('custom.update.record', current, current.sys_id, 'additional_info');
2. Create a Business Rule to listen for the event and perform the update
   current.some_field = 'new_value';
   current.update();
This method updates the record after the insert without triggering recursion issues in health scans.

Another way -
Another way to update the record after insertion without causing recursion is to use setWorkflow(false) within the After Insert Business Rule This will prevent triggering workflows and business rules that could cause recursion. \\
Use setWorkflow(false)` in After Insert Business Rule
You can update the same record without triggering recursion by temporarily disabling workflows and business rules while updating the record.
The consequence of using setWorkflow(false) in your script is that it disables the execution of business rules and notifications during the your process. you are bypassing these additional functionalities and reducing the load on the system, which can help improve performance during bulk record modification.
 
 
Kindly mark it correct and helpful if it is applicable.

Thanks,

Raj

Ankur Bawiskar
Tera Patron
Tera Patron

@MaharshiC 

I agree with Mark here. you can always use Flows

What's your business requirement here?

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