You need to sign in to do that
Don't have an account?
jonpilarski1.3914448382645588E12
How to catch DML Exception in APEX trigger
Hi Everyone
I have a simple before update trigger below. I am trying to catch any error and log to a custom table. The below will cause Duplicate error in UI but nothing is inserted into custome object USD_Error_Log__c.. Its like trigger does not fall into either DMLException or Exception catch blocks. Any and all help is appreciated.
Thanks
Jon
trigger USD_Contact on Contact(before update){
Set<USD_Error_Log__c> student = new Set<USD_Error_Log__c>();
for (Contact c: Trigger.new) {
// delete [select id from USD_Error_Log__c];
try{
c.TargetX_SRMb__BannerID__c = '008372883'; // this will cause Duplicate value on record error
} catch (DMLException e) {
string error = e.getMessage();
student.add(new USD_Error_Log__c(Banner_ID__c = c.TargetX_SRMb__BannerID__c,Error_Message__c = error ));
if(!student.isEmpty()){
List<USD_Error_Log__c> toInsert = new List<USD_Error_Log__c>();
try{
toInsert.addAll(student);
insert toInsert;
}catch(Exception se){
}
}
} catch(Exception e) {
string error = e.getMessage();
student.add(new USD_Error_Log__c(Banner_ID__c = c.TargetX_SRMb__BannerID__c,Error_Message__c = error ));
if(!student.isEmpty()){
List<USD_Error_Log__c> toInsert = new List<USD_Error_Log__c>();
try{
toInsert.addAll(student);
insert toInsert;
}catch(Exception se){
}
}
}
}//end loop
}
I have a simple before update trigger below. I am trying to catch any error and log to a custom table. The below will cause Duplicate error in UI but nothing is inserted into custome object USD_Error_Log__c.. Its like trigger does not fall into either DMLException or Exception catch blocks. Any and all help is appreciated.
Thanks
Jon
trigger USD_Contact on Contact(before update){
Set<USD_Error_Log__c> student = new Set<USD_Error_Log__c>();
for (Contact c: Trigger.new) {
// delete [select id from USD_Error_Log__c];
try{
c.TargetX_SRMb__BannerID__c = '008372883'; // this will cause Duplicate value on record error
} catch (DMLException e) {
string error = e.getMessage();
student.add(new USD_Error_Log__c(Banner_ID__c = c.TargetX_SRMb__BannerID__c,Error_Message__c = error ));
if(!student.isEmpty()){
List<USD_Error_Log__c> toInsert = new List<USD_Error_Log__c>();
try{
toInsert.addAll(student);
insert toInsert;
}catch(Exception se){
}
}
} catch(Exception e) {
string error = e.getMessage();
student.add(new USD_Error_Log__c(Banner_ID__c = c.TargetX_SRMb__BannerID__c,Error_Message__c = error ));
if(!student.isEmpty()){
List<USD_Error_Log__c> toInsert = new List<USD_Error_Log__c>();
try{
toInsert.addAll(student);
insert toInsert;
}catch(Exception se){
}
}
}
}//end loop
}
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_bulk_exceptions.htm
An Introduction to Exception Handling
https://developer.salesforce.com/page/An_Introduction_to_Exception_Handling
Please try dataBase.insert or update like below code. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm
Please modify your code according to above code.
Let us know if that will help u
Thanks
Amit Chaudhary
when i run this
I got this error
i need to check if gen a new id until it's not duplicate, that why i use do while.
We can get only dml exception from parent exception.
Please follow the below link for reference.
https://sfdctechsolutions.blogspot.com/2021/07/show-only-dml-exception-message-from.html