You need to sign in to do that
Don't have an account?
Raj R.
HOw to update two different object in batch class?
Hi,
I have a batch class that is supposed to update the user objet and a custom object called "cobj1".
When the two separate update calls are made, i am getting MIXED_DML_OPERATION" DML operation on setup is not permitted after you have updated a non-setup object. What is the recommended way to update two objects in one batch class?
Sample code:
I have a batch class that is supposed to update the user objet and a custom object called "cobj1".
When the two separate update calls are made, i am getting MIXED_DML_OPERATION" DML operation on setup is not permitted after you have updated a non-setup object. What is the recommended way to update two objects in one batch class?
Sample code:
global class batchJob implements Database.Batchable<sObject>{ global final String Query; global batchJob(String q,){ Query=q; } global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject> scope){ List<cobj1> cobjs = new List<cobj1>(); List<ID> usrIds = new LIst<Id>(); //get list from scope for(sObject sob : scope){ cobj1 cb = (cobj1)sob; //update some field on cb //store user's created by id usrIds.add(cb.CreatedBy); cobjs.add(cb); } update cobjs; List<User> users = [Select Id, Name from user where Id IN: usrIds]; for(User u : users) { //perform some field update u.CustomField = <some new value> } update users; } global void finish(Database.BatchableContext BC){ } }
All Answers
you can't perform DML on setup objects(User object) along with a non-setup object (cobj1).
can you try System.runAs Blocks or @future.
this link is a test method mixed DML but it's still applicable in your case.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects_test_methods.htm
Best Regards