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?

1 REPLY 1

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