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

    sfdcsharepoint.com

    Record Triggered Flow- Summer’20 Salesforce Release Feature

    n

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

    n

    Logic:

      n

    • I decided to create one unique text field on the child object and then update it with a unique key value.
    • n

    • As the field marked as unique if a duplicate record comes with the same key then the system will throw an error message.
    • n


    Solution Spectrum:nI had various options on hand to achieve this but I found the record triggered flow is the best option.n

    Option 1: Formula Field + Using Duplicate and matching rule

    n

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

    n

    Option 3: Text field + Process Builder

    n

    Option 4: Before Event Apex Trigger

    n

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


    Option 1: Formula Field + Using Duplicate and matching rule

    Steps:

      n

    • I created a formula field on the child object to populate (Parent.Id+CurrentRecord.State) Because I wanted unique records by State.
    • n

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

    Outcome:

      n

    • This Solution didn’t work.n
    • n


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

    Steps:n

      n

    • Create a Text field instead of a formula field.
    • n

    • Update field with unique key value using a workflow field update on the child object.
    • n

    • Create duplicate and matching rules to show an error message.
    • n

    Pros:n

      n

    • Easy to configure
    • n

    • User-friendly error message on duplication
    • n

    Cons:n

      n

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

    • Workflows are executed very late in the execution context
    • n

    • Same object update is there hence recursion must be handled.
    • n

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

    Outcome:n

      n

    • Works but not recommended, due to poor performance.
    • n

    n


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

    Steps:n

      n

    • Create a Text field marked as unique.
    • n

    • Update field with unique key value using a Process builder action field update on the child object.
    • n

    Pros:n

      n

    • Easy to configure
    • n

    • The unique field will handle to display the error
    • n

    Cons:n

      n

    • Performance issue — Process Builder
    • n

    • Same object update is there hence recursion must be handled.
    • n

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

    Outcome:n

      n

    • Works but not recommended, due to poor performance.
    • n


    Option 4: Before Event Apex Triggern

    Steps:n

      n

    • Write before insert/update event trigger
    • n

    • throw error from trigger using .addError() standard method.
    • n

    Pros:n

      n

    • Easy to build
    • n

    • Before the event trigger, it is, hence no need to make any DML
    • n

    • No need to worry about recursion
    • n

    Cons:n

      n

    • Test class and Code coverage is required
    • n

    Outcome:n

      n

    • This solution works but a better solution is available 🙂
    • n

    n


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

    n

    Steps:

      n

    • Create a Text field mark it as Unique.
    • n

    • Update field with unique key value using a flow (Before Save Record Triggered)
    • n

    • The system will throw an error preventing duplication.
    • n

    Pros:

      n

    • Easy to configure
    • n

    • Faster performance
    • n

    • No need to worry about recursion
    • n

    • Doesn’t fire any other workflow, process builder or triggers
    • n

    Cons:

      n

    • Not really if you are comfortable building a flow.
    • n

    Outcome:

      n

    • Works very well.
    • n


    Thank you for reading my post, please share your thoughts and comments.

    Leave a Reply

    Your email address will not be published. Required fields are marked *