Same flow triggered by multiple records at the same time resulting in incorrect update
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi Everyone,
We have a flow which runs on sc_task table and in one of the steps the flow is checking if the allocations available on an alm_license record then it will reduce the allocations by 1. This functionality is working fine but it doesnt work correctly when the same flow if triggered by multiple records at the same time to update the same alm_license record.
For e.g. If the allocations available are 2 and 10 records trigger the same flow within few secs and trying to update the same alm_license record, for all 10 records it checks allocations available > 0 condition at the same time so for all 10 tasks the condition satisfies and they all reduce the allocations available by 1 so allocations available goes to -8 which shouldnt happen.
How to prevent parallel flow execution to prevent such scenario?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @chetan17421 ,
Instead of using the Flow Designer to decrement allocations_available, you can:-
1. Create a before update Business Rule on alm_license.
2. Inside the BR, check allocations and decrement atomically.
Example code-
var gr = new GlideRecord('alm_license');
gr.addQuery('sys_id', '<license_sys_id>');
gr.addQuery('allocations_available', '>', 0);// when allocation is not 0
gr.query();
if (gr.next()) {
gr.allocations_available = gr.allocations_available - 1;
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
18 hours ago
@Nawal Singh : What if the same issue is occurring even when the logic is moved to BR?
Also, Why will the same issue not occur in BR?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
18 hours ago
Hi,
This is a tricky one.
Since flows run async you cannot predict the execution/processing time. It will happen when there are available resources.
You can however increase the chance of preventing multiple executions happening at the exactly same time by adding a random wait before the allocation reduction is done.
This thread shows a possible way to add a random waiting time.
Another way would be to check in the [sys_flow_contect] table if there are already any in progress flows (of your current flow sysid) executing, and just wait for them to be completed before starting the execution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
18 hours ago
this link has solution and you can enhance it
it uses mutex and gives exclusive access
Incrementing System Properties integer value by multiple users causing duplicate values
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader