In this article we will learn about What is Mixed DML Operation Error in Salesforce Apex and how to resolve Mixed DML error.
What is Mixed DML Error?
In Salesforce Mixed DML Operation error occurs when you try to perform DML operations on setup objects and non-setup objects in the same transaction. In short you are not allowed to perform DML operations on setup and non-setup objects in a single transaction.
Understanding Setup and Non-Setup Objects
Setup Objects
These objects define metadata or settings in Salesforce. Some common examples are:
- User
- Profile
- PermissionSet
- UserRole
Non-Setup Objects
These objects store business data. Some examples include:
- Account
- Contact
- Opportunity
- Custom objects like Project__c
Example Scenario
Suppose you have a scenario where you want to create a user (Setup) record and an Account (Non-Setup) record in the same transaction. If you try to perform these operations in a single transaction, then Salesforce will throw a MIXED_DML_OPERATION error.
Code Example: Producing a Mixed DML Error
Create an apex class named MixedDMLErrorExample and replace the code with the below code snippet.
public class MixedDMLErrorExample { public static void triggerMixedDMLError() { // Create a new User User newUser = new User( FirstName = 'John', LastName = 'Doe', Email = 'john.doe@example.com', Username = 'johndoe0101@example.com', Alias = 'jdoe', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'UTF-8', ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id, userroleid = [SELECT Id FROM UserRole WHERE Name='CEO' Limit 1].Id, LanguageLocaleKey = 'en_US' ); // Create a new Account Account newAccount = new Account( Name = 'ABC Corp' ); // Attempt to insert both objects in the same transaction insert newAccount; // Non-setup object insert newUser; // Setup object } }
In the above code snippet we are trying to create Account and User together. Let’s execute the class from the anonymous window. Below is the screenshot of the error message you will see.

Now let’s learn how to fix mixed DML operation error in Salesforce.
How to Fix the Mixed DML Error?
We can fix the mixed DML error by utilising future method. Future method runs asynchronously, allowing Salesforce to handle DML operation in seperate transaction. We will move the code inserting the user record in future method which is asynchronous. Now we are performing DML operations in different transaction and hence we will not get any error.
Look at the below code snippet where we have used future method.
public class MixedDMLErrorFixWithFuture { public static void resolveMixedDMLError() { // Create a new Account Account newAccount = new Account(Name = 'ABC Corp'); insert newAccount; // Non-setup object first // Call future method for User insertion insertUserAsync(); } @future public static void insertUserAsync() { User newUser = new User( FirstName = 'John', LastName = 'Doe', Email = 'john.doe@example.com', Username = 'johndoe0101@example.com', Alias = 'jdoe', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'UTF-8', ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id, userroleid = [SELECT Id FROM UserRole WHERE Name='CEO' Limit 1].Id, LanguageLocaleKey = 'en_US' ); insert newUser; // Now runs in a separate transaction } }
If you execute the above code from the anonymous window you will not get any error and the records will be created as you can see below.

Also check out the video on How to fix mixed DML operation error in Salesforce Apex?
Conclusion
A mixed DML Operation error occurs in Salesforce when you try to modify both setup and non-setup objects in the single transaction. To fix this you can use @future methods to separate transactions.
We hope now you know What is Mixed DML Operation error and how to resolve mixed DML Operation error using Future methods. If you have any doubts then let us know in the comment box below.