- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 12:29 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 01:25 AM - edited 12-11-2024 01:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 12:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 12:41 AM
Hi @MaharshiC
-
Use the setWorkflow() methodSet 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 ruleMake 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 RuleThis 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
********************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 12:47 AM
Hi @MaharshiC
To avoid recursion in an After Insert Business Rule use events to update the record
Another way -
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.
Thanks,
Raj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 01:02 AM
I agree with Mark here. you can always use Flows
What's your business requirement here?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader