21

I want to check whether a certain criteria is met or not using a trigger before inserting a record. if it meets the criteria, i insert the record, but if it does not i want to display an error message that it doesn't meet the criteria. how do i do it? Please help if anyone knows.

  • 2
    This is a great question, though maybe worth clarifying in the title a little further what your asking, this will help attract more people both now and in the future to get the benefit of both your question and answers. Something like "How to prevent a record being inserted if criteria ain't met for before insert?" – Andrew Fawcett Oct 16 '13 at 11:46
  • 2
    @AndrewFawcett I was thinking along the same lines, although your proposed revision is only slightly less confusing. How about: "How do I use a trigger to prevent new records from being created when some criteria is not met?" – sfdcfox Oct 16 '13 at 16:15
  • Yes works for me! Good suggestion! – Andrew Fawcett Oct 16 '13 at 17:27
  • I give this question an upvote just for the refreshing use of the word ain't. – Shane McLaughlin Oct 17 '13 at 3:00
  • 2
    Don't forget about validation rules. Depending on your criteria, you may be able to use them instead. Config before code is always easier to maintain in the future. – Daniel Hoechst Oct 17 '13 at 3:24
22

You can use the addError method on the SObject for this, the Trigger Exceptions topic of the Apex Developers guide starts to describe the process and links to other topics describing this approach.

Marks a record with a custom error message and prevents any DML operation from occurring.

Note that you can also apply the addError method call to a field, as well. This will also block the insert of the record and provide the user with more context of what field value is causing the issue.

Trigger.new[0].myField__C.addError('bad');

Here is sample showing the difference between record level and field level use of addError

trigger TestTrigger on Test__c (before insert) {

    for(Test__c record : Trigger.new)
    {
        if(record.Name.contains('Bad'))
            record.addError('Cannot have the word Bad in the record Name');
        if(record.A_Number__c != 42)
            record.A_Number__c.addError('You have not provided the answer to life the universe and everything!');
    }
}

enter image description here

enter image description here

Security Note: If you worried about other Apex Triggers being added to your object and changing the values of your fields. You should consider performing the validation in the after phase of the trigger since only then can you ensure no further changes will be made to your fields, by the user or other Apex Triggers added by the customer (if your developing a packaged solution) or other developers.

Update: I have since written a blog on the practice of considering validation in the after phase of your trigger.

  • Thanks Andrew. This is the trigger i'm writing. trigger duplicateLead on Lead (Before insert) { Lead lead1=trigger.new[0]; List<lead> list1= [Select id,email from lead where emailid=:lead1.emailid]; List<opportunity> list2= [Select id,email from opportunity where emailid=:lead1.emailid]; List<Account> list3= [Select id,email from account where emailid=:lead1.emailid]; if(list1.size==0 && list2.size==0 && list3.size==0){ update lead1; } else{ } } I need to know what shall i write in the else block that informs the user that your record cannot be inserted. – Sam Oct 16 '13 at 10:43
  • I've updated my answer with a sample peace of code that will help you with this and some screenshots to illustrate the results. Hope this helps! – Andrew Fawcett Oct 16 '13 at 10:48
  • Your very welcome, its a great question, i've proposed a small way to improve it for future travellers to this site! – Andrew Fawcett Oct 16 '13 at 11:47
  • What if you were inserting a batch of records and wanted the entire batch to fail on one bad record? – Aequitas Apr 3 '18 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.