Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Same flow triggered by multiple records at the same time resulting in incorrect update

chetan17421
Tera Guru

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?

6 REPLIES 6

Nawal Singh
Tera Guru

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();
}

@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?

OlaN
Tera Sage
Tera Sage

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.

Ankur Bawiskar
Tera Patron
Tera Patron

@chetan17421 

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! 🙏

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