+ Start a Discussion
surendra challapallesurendra challapalle 

what is MIXED DML operation?

Nirdesh_BhattNirdesh_Bhatt
Hi Surendra,
There are two possibilities so you are not getting exception 
1) I think you are not getting Mixed DML Operation because after insert and before update are two different transactions, Mixed DML operations are not allowed in single transaction (check debug logs if u have CODE_UNIT_STARTED 2 times under event tab)
2) Assuming you are not inserting or updating any other sobject other than user in after insert and before update trigger then  there is no question of getting Mixed DML operation exception as you are not combining setup object with non setup objects.

Thanks,
Nirdesh
 
buggs sfdcbuggs sfdc
Hi

If we perform DML operation on standard/custom object and global objects(User, UserRole, Group, GroupMember, Permission Set, etc...) in same transaction this error will come.
To avoid this error, we should perform DML operation on standard/custom object records in a different transaction.
In general all the apex classes and apex triggers execute synchronously (execute immediately).
if we perform DML operation on standard/custom object records asynchronously (execute in future context), we can avoid MIXED-DML-OPERATION error.
To execute logic asynchronously keep the logic in an apex method (in a separate apex class, not in same apex trigger) which is decorated with@future annotation.

see the below example -
public class TriggerUtility {
 /*
 1. Following future method execute asynchronously (whenever server is free it will execute in future context).
 2. We should not declare @future method in Apex Trigger.
 3. @future method should be always static.
 4. @future method accepts only primitive data types (Integer, String, Boolean, Date, etc...) as parameters and it won't accept 
 non-primitive data types (sObject,Custom Objects and standard Objects etc.. ) as parameters.
 5. @future method should not contain return type. Always it should be void.
 6. From an apex trigger we can make only make asynchronous call outs. To make call out we should include "callout = true" beside the future @annotation.
 7. We cannot perform synchronous call outs from Apex Trigger.
 */
 //Below is the example for the future method -
 @future(callout = true)
 public static void processAsync(primitive parameters) {
  //Logic to insert/update the standard/custom object.
 }
}

Thanks!
Mark it has correct Answer if t helps you,it may help to others even!
Arun Garg 9Arun Garg 9
Best Answer About Mixed DML Error and How To prevent MIxed DML Error using Future Method (http://www.salesforceadda.com/2017/08/future-method-and-prevent-mixed-dml.html)
Mohana Priya SwamiappanMohana Priya Swamiappan
I am getting an error even though i use @future annotation
Suraj Tripathi 47Suraj Tripathi 47
Hi Surendra,

Greetings!

Mixed DML operations inside one transaction aren’t allowed. You can’t perform DML on a setup sObject and another sObject within the same transaction. However, you'll perform one style of DML as a part of the Associate in Nursing asynchronous job and also the alternatives in other asynchronous jobs or within the original transaction.

Please mark it as the best answer if it helps you to fix the issue.

Thank you!
Regards,
Suraj Tripathi