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?

8 REPLIES 8

@chetan17421 

Thank you for marking my response as helpful.

💡 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

MaxMixali
Mega Sage

 

 

Solution:
Replace your Flow Designer update action with a Run Script action containing:

javascript(current)
var license = new GlideRecord('alm_license');
license.setWorkflow(false);
if (license.get('sys_id', inputs.license_sys_id)) {
if (license.getValue('allocations_available') > 0) {
license.allocations_available = parseInt(license.allocations_available) - 1;
license.update();
outputs.success = true;
} else {
outputs.success = false;
}
}

setWorkflow(false) prevents recursive flow triggers, and the atomic read-modify-write operation in a single script prevents race conditions. The database row lock during get() and update() ensures concurrent executions process sequentially.

 

 

chetan17421
Tera Guru

@MaxMixali : Thanks for your reply. If two flows are triggered at the same time updating the same record, Which statement/part of code here will prevent the another flow execution from running in parallel?

 

Could you please explain, as I didn't get it exactly.

How It Works

When a flow executes an Update Record action:

  1. ServiceNow acquires a database row lock on that specific record
  2. This lock is held for the entire duration of the update transaction
  3. Any other flow/process trying to update the same record must wait until the lock is released
  4. The second flow then proceeds with its update based on the current state of the record (which now includes changes from the first flow)

No Specific Code Required

 

I hope it's. ore clear now