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

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.

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 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.

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.

Pros:

  • Easy to configure

Cons:

  • Performance issue — Rules on child records, will execute for all records under the parent.

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.

Pros:

  • Easy to configure

Cons:

  • Performance issue — Process Builder

Outcome:

  • Works but not recommended, due to poor performance.

Option 4: Before Event Apex Trigger

Steps:

  • Write before insert/update event trigger

Pros:

  • Easy to build

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.

Pros:

  • Easy to configure

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

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