[SalesForce] Visual Workflow – Duplicate IDs in Collection Variable

My flow is failing to trigger due to this error:

"Sandbox
Encountered unhandled fault when running process Data_com_Get_Contacts_Constraint/301J00000000idC exception by user/organization: 00DJ0000003OapW/{4} Source organization: 00DU0000000J4OM (null) Two or more items in this collection have the same ID value. Make sure each ID only appears once.
caused by element : FlowRecordUpdate.Update_Collection
caused by: Two or more items in this collection have the same ID value. Make sure each ID only appears once.
Salesforce Error ID: 387194366-108055 (-265105789)"

My flow is trigger by a checkbox field, does a Fast Lookup on Accounts that have that field checked and saves them to a Collection Variable. I then Loop through that Collection Variable and assign fields based on certain criteria, saving them to a new Collection Variable. I then perform a Fast Update using the second Collection Variable.

I'm just testing it on one account and no others meet the criteria which means it must be adding the account multiple time to the Collection Variable. Is there a way to control for this? Shouldn't a single Loop only loop through each record once?

Best Answer

My apologies for my comment suggesting you use a set, as you can't specify the use of a set in a Visual Workflow.

You can however use the contains operator in Decision elements to determine whether an item in a collection variable contains the exact same value as a specified variable, or whether an item in an sObject collection variable contains the same exact field values as a specified sObject variable before adding them to a collection.

You'll want to do this sort of testing before adding sObjects from one collection to another collection to avoid this issue. See Flow Design Considerations for more on this.