0

I have written a trigger logic in that I want to throw the error from trigger, but its not working.

for(Case cs : cases){
   ............
   ............  
 }

        if(contactIds.size() > 0){
            List<Case> caseList = [SELECT Id, Contact.Name,contactId 
                                FROM Case
                               WHERE CreatedDate = THIS_QUARTER 
                                AND isOldRequest__c='Yes'
                                AND ContactId IN :contactIds];
            if(caseList.size() > 20){
                for(Case c : caseList){
                    c.addError('XXXXXXX');
                } 
            }
        }

c.addError('XXXXXXX'); Doesn't throw error. I dont want that Contact to create 21st record if its a OldRequest.

  • 2
    First, you are executing a SOQL query in a for loop which is a bad practice. You should start by reading this tutorial. Then, are you sure the size of your caseList is superior to 20? – Martin Lezer Aug 10 '17 at 12:13
  • When used on Trigger.new in before insert and before update triggers, and on Trigger.old in before delete triggers, the error message is displayed in the application interface. – NITHESH K Aug 10 '17 at 12:13
  • 1
    Oh yes sorry. The indentation fooled me. – Martin Lezer Aug 10 '17 at 12:15
  • @NITHESH K - I dont understood, Could you please show some code? I am using Trigger.new, Trigger.oldMap, but still not working ! trigger CaseTrigger on Case (before insert, before update, after update) – user4567570 Aug 10 '17 at 12:15
  • Can u add more code from where you getting cases – NITHESH K Aug 10 '17 at 12:20
0

If you add directly addError method to Case once you received from trigger.new it will work. something like this.

Normally this is the way Trigger Exception will work

trigger CaseException on Case(before insert,before update) {
    for(Case c : trigger.new)
    {
        if(c.isOldRequest__c ==null) // some condition on your requirement
        {
          c.addError('please insert the field');
        }
    }
}

Your below code will not work because your caseList not under the scope of trigger insert or update. No where else your inserting or updating again those SOQL caseList. unless your insert or update on those object it will not trigger any exception on those SOQL object.

 if(contactIds.size() > 0){
                List<Case> caseList = [SELECT Id, Contact.Name,contactId 
                                    FROM Case
                                   WHERE CreatedDate = THIS_QUARTER 
                                    AND isOldRequest__c='Yes'
                                    AND ContactId IN :contactIds];
                if(caseList.size() > 20){
                    for(Case c : caseList){
                        c.addError('XXXXXXX');
                    } 
                }
            }

Trigger exception is raised when addError method is present on Trigger.new or Trigger.old Objects when u inserting,updating and delete the objects.

You can refer some question Arise in stack

More about addError

| improve this answer | |
  • Hey, Thanks I just tackle the condition, as per your guidance, its working now – user4567570 Aug 10 '17 at 12:31
  • You have to add your "addError" to SObject you received from Trigger, After trigger is going insert only those record. Not the record your SOQL in trigger. – NITHESH K Aug 10 '17 at 12:34
  • Could you please show some code for better understanding ? – user4567570 Aug 10 '17 at 12:35
  • @user4567570 Your Welcome .. – NITHESH K Aug 10 '17 at 12:35

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.