- August 23, 2016 at 3:23 pm #15446Tanu #391
How to apply uniqueness on Junction object?
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?August 26, 2016 at 10:36 am #15644Mohit #403
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.September 30, 2018 at 4:51 am #41449Avnish Yadav #3
You could create a trigger that queries for other junction objects that have the same combination of A and B.September 30, 2018 at 4:51 am #41450
Please Register to reply to this topic. /