• #15446
    Salesforce | Tanu Tanu #391

    How to apply uniqueness on Junction object?

    Hii All,

    I have a junction object C of A and B.

    I found out that I can create multiple entries of A and B in Object C – this is wrong, as in my case, each combination of A and B should appear only once in Junction object C. The second time, someone will try to enter A and B as a new entry in C – it should fail.

    How can I enforce this?


    Salesforce | Mohit Mohit #403

    Hi Tanu,

    Create a text field of (External Id, Unique) on the junction object.

    Create a workflow on C to fire Everytime the record is created or edited (to account for cases where junction object records are reparented) to set this field as the concatenation of the Ids of A and B records that the C record is relating.

    This workflow trying to set the composite primary key field will generate an error if a duplicate record is being inserted.

    You can add to the composite key to include any other uniqueness criteria such as relationship type if you have that notion.

    The workflow will fire only for new records created, you will need to retrospectively populate for already existing records possibly by extracting and concatenation via data loader, or simply do a phantom update on the junction records and that should cause the workflow to fire and set the key. However this could also throw some errors for any existing duplicates which you may need to resolve manually.


    You could create a trigger that queries for other junction objects that have the same combination of A and B.


    Another solution would be to create a validation rule on object C which will do a VLOOKUP on A and B based on the populated lookups and also exclude the record itself. You can also handle this with a nicer error message.

Viewing 4 posts - 1 through 4 (of 4 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?