Record Triggered Flow trying to create duplicate records

after-triggerbulkificationflow-interviewsrecordtriggeredflowvisual-workflow

I have a record triggered flow that needs to create related records if they do not already exist. However, once the record is created, if other records in the transaction would have triggered a create records, the flow is still trying to create a record, even after it has already created one that should be used instead.

For example:

Imagine you have two objects: ObjA and ObjB, where ObjA has unique field that identifies a specific ObjB record. If 3 ObjA records are inserted, with an ObjB identifier of 'ABC', the flow would query for ObjB records that are named 'ABC', and if it cannot find any records, it must create a records named 'ABC'. When the flow gets to the point of the Create Records, it creates the record the first record, but then tries to create the record again for the remaining two records.

I assume this is because once each interview has knowledge of if a record exists or not, all of the interviews that need to create a record 'wait' at the create records and then execute at the same time, therefore attempting to create duplicates.

Is there anyway to get flow interviews to communicate with eachother to only execute the create records once and then the flow could not create duplicates?

Thank you!

Best Answer

You said:

I assume this is because once each interview has knowledge of if a record exists or not, all of the interviews that need to create a record 'wait' at the create records and then execute at the same time, therefore attempting to create duplicates.

This is basically correct, each Flow Interview executes independent of each other, coming together only at points of bulkification (GET, Update, Create, Delete, and Action) elements.

The way I would solve this is use an Apex Action. This action will take as an argument a List where each list element corresponds to one Flow Interview's ObjA.

The Apex would pivot the incoming list based on ObjA.FieldX to get unique values, creating only the necessary ObjB and associating the ObjB.Id into the corresponding ObjAs' lookup field.

If the Flow needs to know the ObjB then the invocable Apex returns a list of the same arity as the incoming list of ObjA and in the same order as the ObjA's. Example:

incoming ObjA
XXX.vv
XXY.ww
XXZ.vv

construct two ObjB - BBA with value vv and BBB with value ww

return
BBA
BBB
BBA