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
MktdevMktdev 

Mixed_DML_operation with Batch Apex

Hi,

 

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); 
ExpectionTrackingCls.getErrorStatus(lstUserToUpdate,updateResult); 

 

ExpectionTrackingCls

 

 

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();
					newErrorRecord.name = lstUser[i].username;
					newErrorRecord.message__c = err.getMessage()+err.getStatusCode();
					errObj.add(newErrorRecord);
				}
            }
        }
	}

 

 

 

 

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

Thanks!

All Answers

MktdevMktdev

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

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

Thanks!
This was selected as the best answer
sivaextsivaext

Hi Mktdev,

 

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

 

Thanks 

Siva