How to fix MIXED_DML_OPERATION in Salesforce deployments
Apex tries to modify a setup object, like User or Group, and a non-setup object in the same transaction.
Surfaces during: Apex runtime, most often inside test execution required for a deployWhat it means
MIXED_DML_OPERATION is an Apex runtime exception: Salesforce doesn't allow DML on a setup object (User, Group, GroupMember, and similar) and a non-setup object (Account, Contact, custom objects) within the same transaction. It most often surfaces during the Apex test execution a production deployment requires, turning into a deployment-blocking test failure.
The restriction exists because setup objects can change record-level access and sharing, and Salesforce won't let a single transaction both alter who can see data and write that data at the same time, which is why the fix is always about transaction boundaries, never about the records' content.
Common causes
The fix
- Move setup-object DML into its own transactionWrap the User or Group insert in a future method, or otherwise isolate it, so it commits before the business-object DML runs.
@future private static void insertUserAsync(String jsonUser) { User u = (User) JSON.deserialize(jsonUser, User.class); insert u; } - Use Test.startTest() and Test.stopTest() to split the transactionIn tests, place the setup-object DML before Test.startTest() so it runs in its own transaction boundary before the business-object DML.
- Never combine setup and business object inserts in one trigger contextRefactor automation so setup-object changes, like user provisioning or group membership, never share a transaction with business-record DML.
How Serpent prevents this
Serpent's CI pipeline runs the full Apex test suite on every task before it's eligible to merge, so a MIXED_DML_OPERATION test failure surfaces on the task that introduced it, not on a production release. See the Salesforce deployment error library.

Prevention
Related errors
MIXED_DML_OPERATION, answered
Start free. No credit card, no install, no commitment.
Set up in under 15 minutes. No DevOps hire needed.
