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

Mixed_DML_operation with Batch Apex



I am trying to update user in a batch apex and when there is any error I am trying capture those and store in a custom object but I am getting Mixed DML operation error.


Here is the flow:

  • Contact Updated during the course of the day
  • All contacts are flagged for nightly processing
  • At the mid night my Batch apex picks up those contact and then verify existence of portal user for them.
  • If portal user exist and there is change in the email then i want to update username and email both
  • If any error occur like Username already exist, duplicate user etcs I want to track in custom object.

Please suggest.


Snip of code:


Batch Apex: 


Database.SaveResult[] updateResult = Database.update(lstUserToUpdate,false); 





public static void getErrorStatus(List<User> lstUser,LIST<Database.SaveResult> dbSaveResult)
        List<Errorlog__c> errObj=new List<Errorlog__c>();
        for(Database.SaveResult sr : dbSaveResult){ 
            if (sr.isSuccess()==false) 
				for(Database.Error err : errs)
					Errorlog__c newErrorRecord = new Errorlog__c(); = lstUser[i].username;
					newErrorRecord.message__c = err.getMessage()+err.getStatusCode();





Best Answer chosen by Admin (Salesforce Developers) 
I have resolved it on my own by inserting the error logs in finish method.


All Answers


No,We cant call @future method from batch apex as batch apex it self is asynchronous call.

I have resolved it on my own by inserting the error logs in finish method.

This was selected as the best answer

Hi Mktdev,


I am also facing same issue, can you please explain how to capture error logs in finish method?