You need to sign in to do that
Don't have an account?
niki 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');
}
}
}
}
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');
}
}
}
}
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
Let me know if it helps
if you want to display the error message, replace you catch method with following and check your debug log.
let me know if it helps you and marking it as best.
Thank You
In the controller class add the error message where required. Here is a sample code
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