How to prevent duplicate child records for the same parent record?

Tejas Kapasi
3 min readJan 1, 2021

Record Triggered Flow- Summer’20 Salesforce Release Feature

Duplicate Message

Requirement:
I have a business use case where I need to restrict users from entering duplicate child records for the same parent record.

Logic:

  • I decided to create one unique text field on the child object and then update it with a unique key value.
  • As the field marked as unique if a duplicate record comes with the same key then the system will throw an error message.

Solution Spectrum:

I had various options on hand to achieve this but I found the record triggered flow is the best option.

  • Option 1: Formula Field + Using Duplicate and matching rule
  • Option 2: Text Field + Workflow Field Update + Duplicate and matching rule
  • Option 3: Text field + Process Builder
  • Option 4: Before Event Apex Trigger
  • Option 5: Before Event, Record Triggered Flow (Summer’20 Release)

Option 1: Formula Field + Using Duplicate and matching rule

Steps:

  • I created a formula field on the child object to populate (Parent.Id+CurrentRecord.State) Because I wanted unique records by State.
  • Then I tried to use this field in the Matching rule to find duplicates, but it's not available there because of the formula field.

Outcome:

  • This Solution didn’t work.

Option 2: Text Field + Workflow Field Update + Duplicate and matching rule

Steps:

  • Create a Text field instead of a formula field.
  • Update field with unique key value using a workflow field update on the child object.
  • Create duplicate and matching rules to show an error message.

Pros:

  • Easy to configure
  • User-friendly error message on duplication

Cons:

  • Performance issue — Rules on child records, will execute for all records under the parent.
  • Workflows are executed very late in the execution context
  • Same object update is there hence recursion must be handled.
  • Because of this update if there are any other workflows or process builders or triggers are there on the same object then those will be executed.

Outcome:

  • Works but not recommended, due to poor performance.

Option 3: Text field (Mark it as Unique) + Process Builder

Steps:

  • Create a Text field marked as unique.
  • Update field with unique key value using a Process builder action field update on the child object.

Pros:

  • Easy to configure
  • The unique field will handle to display the error

Cons:

  • Performance issue — Process Builder
  • Same object update is there hence recursion must be handled.
  • Because of this update if there are any other workflows or process builders or triggers are there on the same object then those will be executed.

Outcome:

  • Works but not recommended, due to poor performance.

Option 4: Before Event Apex Trigger

Steps:

  • Write before insert/update event trigger
  • throw error from trigger using .addError() standard method.

Pros:

  • Easy to build
  • Before the event trigger, it is, hence no need to make any DML
  • No need to worry about recursion

Cons:

  • Test class and Code coverage is required

Outcome:

  • This solution works but a better solution is available :)

Option 5: Before Event, Record Triggered Flow (Summer’20 Release)

Steps:

  • Create a Text field mark it as Unique.
  • Update field with unique key value using a flow (Before Save Record Triggered)
  • The system will throw an error preventing duplication.

Pros:

  • Easy to configure
  • Faster performance
  • No need to worry about recursion
  • Doesn’t fire any other workflow, process builder or triggers

Cons:

  • Not really if you are comfortable building a flow.

Outcome:

  • Works very well.

Thank you for reading my post, please share your thoughts and comments.
Do visit: https://sfdcsharepoint.com

--

--

Tejas Kapasi

6X Salesforce Certified, JDF18 Speaker, Lightning Champion, 4X Ranger