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
niki s 7niki s 7 

How to handle custom validation in trigger

Hello,
I have created two field called num and opp in contact and account object. Created a trigger on account object and upon updating field on account it will also populated on contact object. But there is custom validation on num field . Num should be less than 50.  So while updating account it's give the dml exception .Now my requirement is that I want to display the error message on while updating account. 

trigger AccountUpdate on Account (after update) {
List<Account> parentOppList =[select Id,lookup_opp__c,test__c,Num__c,(select id,test__c,Num__c from contacts) from Account where Id in : trigger.new ] ;      
       List<Contact> oppList = new List<Contact>();
        for(Account parOpp : parentOppList)
        {
            System.debug('inside for '+parOpp.lookup_opp__c);
       
             System.debug('inside for '+parOpp.lookup_opp__c);
             for(Contact childContacts : parOpp.contacts ){
            Contact opp = new  Contact();
            opp.lookup_opp__c = parOpp.lookup_opp__c;
            opp.id=childContacts.id;
            opp.test__c = parOpp.test__c;
            opp.Num__c = parOpp.Num__c;
            System.debug('inside for '+opp.lookup_opp__c);

            oppList.add(opp) ;
         
       // }
    try{
             if (!oppList.isEmpty()){
                 update oppList;
         System.debug('inside catch');
          }
           }
               catch(Exception e)
                 {
                 System.debug('inside catch');
                             
                         throw new DmlException('This is bad number');    
                        // Trigger.New[0].addError('Write error message');

                     }
    }

           
         }
                     
}
ShirishaShirisha (Salesforce Developers) 
Hi Niki,

Greetings!

You need to use try catch block for the update code functionality as suggested here.

https://salesforce.stackexchange.com/questions/101755/how-to-handle-error-thrown-by-validation-rule-when-a-trigger-fires

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
 
AnudeepAnudeep (Salesforce Developers) 
You need to use addError as advised in this post

Let me know if it helps
ravi soniravi soni
hi  niki s 7,
if you want to display the error message, replace you catch method with following
 catch(Exception e)
                 {
                 System.debug('inside catch');
                             
                        System.debug('The following exception has occurred: ' + e.getMessage());

                     }
and check your debug log.
let me know if it helps you and marking it as best.
Thank You
niki s 7niki s 7
Hi thanks everyone .. I want to display error message in the page . AddError message is not working. Can body suggest me how to display the message in page.. in debug log I am able to view the error that error I just want to display in page
AnudeepAnudeep (Salesforce Developers) 
If you want to display an error message on the page. On the Visualforce page please add the tag:
 
<apex:pageMessages />

In the controller class add the error message where required. Here is a sample code
 
if ( requiredFieldName == null){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter a value in the Required Field'));
}

Please see the documentation to learn more. Also, refer to this blog post for sample code

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you