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

Prevent duplicate records in custom object

I have a custom object. There are combination of fields like month_c,Project_c,contact_c and role_c which determine the record as unique.

I can write a trigger on before insert to check if there are any records with the same combination already existing. The question i wanted to ask was

how can i make the insertion stop. once i find there is already an record then it should just not insert the record. it doesnt need to throw / show an error.



Best Answer chosen by Admin (Salesforce Developers) 

If you are writting trigger and you find the record with same combination you can just add error to the record on which trigger has fired and that will stop the further processing. Just search for adserror() method.


Other option can be crating a unique value field on that object and from trigger assign the value to that field in the combination you want. As unique field itself will not allow storing the another record storing same combination will throw error.


I hope this will help.