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.
Thanks
Prady
Although others have answered this with better solutions (generally non-code solutions are more flexible for the end users of the system), the answer to stop a particular record from being inserted is to add an error to a field on that record.
For instance, if I was inserting accounts I might have something like this:
for(Account sAcct : trigger.new)
{
if(sAcct.Name == 'DeniedAccount')
{
sAcct.Name.addError('This is a denied account.');
}
}
Another alternative is to create a class extending Exception
, and then throw a new instance of that exception class, this will prevent all of the records passed to the trigger from being processed as opposed to specific records.