You need to sign in to do that
Don't have an account?

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); } } } }
Thanks!
All Answers
http://www.tgerm.com/2012/04/mixeddmloperation-dml-operation-on.html
Hope this helps you.
No,We cant call @future method from batch apex as batch apex it self is asynchronous call.
Thanks!
Hi Mktdev,
I am also facing same issue, can you please explain how to capture error logs in finish method?
Thanks
Siva