Activity Forums Salesforce® Discussions How to apply uniqueness on Junction object in Salesforce?

  • Mohit

    Member
    August 26, 2016 at 10:36 am

    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.

  • Avnish Yadav

    Member
    September 30, 2018 at 4:51 am

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

  • Avnish Yadav

    Member
    September 30, 2018 at 4:51 am

    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.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos