I wrote this code in a trigger so addError works fine, but when code fails within a try-catch block i expected my addError message but i getting a error as i not using the try-catch.
public with sharing class triggerHandler{
public static void afterUpdate(List<Opportunity> Trigger.new)
CustomObject__c ob = [SELECT id, name, code__c from CustomObject__c where Opportunity__c In :(Trigger.new) ];
ob.code__c = 12345;
try{
update ob;
}catch(DmlException e){
for(Oppotunity opp : Trigger.new){
opp.addError('Show a error');
}
}
}
But i getting an error like: We hit a snag. Review the errors on this page. OppotunityTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0J1w000006574PEA2; first error: Your code number is wrong.
my issue is i expected "Show a error" message when i got a DmlException because is within catch but i getting the whole exception message.
sorry my english help please!!