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
AhmedPotAhmedPot 

Trigger.addError not allowing me to insert records in custom setting.

Hi,

 

I want to insert error details in Custom setting. I have called future method from tigger to perform insert operation followed by trigger.addError. But nothing seem to be inserted in the custom setting.

 

Below are excerpt from Trigger

 

 

trigger AccountSoftValidation on Account (before insert) {

   for(Account acc: [select name from Account where name like:AccountName and Mailing_Local_Country__c=: Trigger.new[0].Mailing_Local_Country__c
   and Mailing_Local_City__c=:Trigger.new[0].Mailing_Local_City__c ]){
    recordsFound = true;
    DuplicateAccountController.insertDuplicateRecords(); // calling future method
    //ErrorMessage += 'Account '+acc.name + '--';
    count +=1;
   }
   ErrorMessage += ''+count+' duplicate Accounts found with same Name, City and Country. ';
   ErrorMessage += 'Please click on link on left side under Helpful Section to view details.';
   if(recordsFound){
        
    Trigger.new[0].addError(ErrorMessage);
   }
  }
 //}
}

 

Future method
 @Future(callout=true)
 public static void insertDuplicateRecords(){
  List<Duplicate_Records__c> duplicateRecords = Duplicate_Records__c.getall().values();
  Database.delete(duplicateRecords,false);
  Duplicate_Records__c dup = new Duplicate_Records__c(name=userInfo.getUserId()+'Account duplicate', User_Id__c = userInfo.getUserId(),
             Type__c = 'Account', status__c = 'duplicate');
  System.debug('dup---'+dup);
  Database.insert(dup,false);
  System.debug('dup---'+dup);
 }

 

 

Is there some way of inserting records in custom object or custom setting while using addError method in trigger?

 

Thanks,

Ahmed

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I'm afraid that there isn't.  Using trigger.addError causes the entire transaction to be rolled back, which will remove any emails, @future calls, scheduled apex etc.  The only trace of your work is in the system log.  We've hit this a few times in the past, normally when we want to create our own logging which holds details longer than Salesforce will.  If there's an error, nothing gets committed.

All Answers

bob_buzzardbob_buzzard

I'm afraid that there isn't.  Using trigger.addError causes the entire transaction to be rolled back, which will remove any emails, @future calls, scheduled apex etc.  The only trace of your work is in the system log.  We've hit this a few times in the past, normally when we want to create our own logging which holds details longer than Salesforce will.  If there's an error, nothing gets committed.

This was selected as the best answer
AhmedPotAhmedPot

Ohk..Well I need to check for other work around.

Thanks for quick reply.

Piyush OjhaPiyush Ojha
Did you find any workaround for the same... or any progress @AhmedPot ?