• Jade Manapee
  • NEWBIE
  • -1 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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



}