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

@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
Kilo 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.