function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Glenn DalyGlenn Daly 

Junction Object Record Uniqueness

I have three objects. Accounts, Consortiums, Campuses and have set up a many to many relationship between Consortiums and Accounts. And also from Campuses and Accounts. I've done this by creating two junction objects that sit inbetween. This enables me to add more than one consortium to an account, and more than one account to a consortium. I am looking for ways to prevent duplicate records being created. Can I do this by validation rule, or will I need to write a trigger?
Best Answer chosen by Glenn Daly
Martijn SchwarzerMartijn Schwarzer
Hi Glenn,

You will have to write an Apex Trigger for this purpose. On Master-Detail relationships you cannot set the Unique flag, and validation rules only look at the current record and not the other records in the database.

You will want to create a before insert trigger, that checks if there is already a record with the same values. If so, you add an error to the duplicate fields and therefore prevent saving the record to the database.

Hope this helps! If you need further assistance, please let me know. I'd be happy to help!

Best regards,
Martijn Schwärzer