Flow Designer: flow running simultaneously, causing duplicate unique ID in custom record

Muhammad Salar
Giga Sage

Hi All,

I am using the Create Record action in Flow Designer to insert records into a table. To set a unique ID for each record, I call a Script Include. This Script Include queries the same table to find the last used unique ID, then generates a new unique ID by incrementing that value by one. However, when multiple flows run simultaneously, this approach results in duplicate unique IDs being created.

How to prevent it?

1 ACCEPTED SOLUTION

@Muhammad Salar 

you can't make flow run sequentially when multiple records are inserted.

the only way to achieve is make that field unique and use before insert business rule to set the value.

Don't set value via flow on that field.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

8 REPLIES 8

@Bhuvan  
I am just creating a record by flow designer create record action, for unique id field i am calling a script include to get new unique id which is working fine.

In script include, i am getting last record unique id by querying the same table.
Issue occurs when same flow running for different ritms simultaneously, it is causing same unique id becasue both flows are querying same last unique id becasue both running at same time.

For unique id field, i have different prefixes and number sequences for different types of records that's why i am not using auto numbering.

I hope you understand the issue now



Ankur Bawiskar
Tera Patron
Tera Patron

@Muhammad Salar 

that's bound to happen as the process is asynchronous and each flow context runs independently.

why not use before insert Business rule for this and handle the logic to generate the unique code and then set it?

-> why not make that field as Unique at dictionary level?

-> this ensure no duplicate insert happens but won't prevent the concurrency race condition

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar 
If we make field unique then race condition still trigger and script will create same unique id for both records, that will also cause issue i think.

What about before update BR because we have to create record from flow, if we update unique id field from before update BR, will it work? keeping in mind the same race condition for inserts from flow?

Or can we stop execution of flow instances or make it run once at a time?

Or can we stop querying the same record until first query is completed etc ??


@Muhammad Salar 

you can't make flow run sequentially when multiple records are inserted.

the only way to achieve is make that field unique and use before insert business rule to set the value.

Don't set value via flow on that field.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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