We typically validate our source control nightly and run all tests. This is essentially a validate only deployment running all tests against production. All tests pass.

However, if we were to run tests through Apex Test Execution or through Dev Console/IDE in production, there are several test methods that fail with a MIXED_DML_OPERATION.

The exception is correct, we can update the methods to use System.runas() and get the test methods working correctly, but I'm more interested in whether this is expected? I saw this previous question about differences when running tests in Mavenmate, but this is different with running a test method on its own vs. in a deployment/validation.

I know more CPU time is allocated for tests running in a deployment context vs. running the test on its own so I suspect this might be another thing Salesforce treats differently in deployments.

However, I can understand the difference in CPU time whereas I can't quite understand this behavior.

I also tested in a sandbox using the code example in their documentation. I removed the System.runAs() so it should fail. This test fails in Dev Console, but passes in a deployment/validation.

private class MixedDML {
    static testMethod void mixedDMLExample() {  
        User u;
        Account a;
        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
        // removed System.runas() to throw mixed_dml
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        UserRole r = [SELECT Id FROM UserRole WHERE Id = 'Id here'];
        u = new User(alias = 'jsmith', email='jsmith@acme.com', 
            emailencodingkey='UTF-8', lastname='Smith', 
            localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
        insert u;
        a = new Account(name='Acmetest123');
        insert a;

enter image description here

enter image description here

enter image description here

Edit: I've created a support case as well

  • odd -- I've never seen this
    – cropredy
    Mar 28 '20 at 17:23

Received the following knowledge article from Support

During deployments we do not run mixed DML operation checks, whereas via the UI we do (either 'Run All Tests' or running a test for an individual class). If the differences between the test failures seen in the UI compared to the deployment is solely down to 'MIXED_DML_OPERATION' errors, then this can be deemed expected behavior as we intentionally do not check these in a deployment.

Apparently, they do not run MIXED_DML checks for deployments and the difference I'm seeing is expected.


As a best practice, do not ever put an on creation trigger or automation on the User object. A scheduled task or repetitive batch is the safest way.

I'd even say not to put an asynchronous automation on the user object.

  • Not sure if I agree here.
    – Adrian Larson
    Mar 31 '20 at 2:33
  • I wasn't asking about best practices on automation for the User object. Mar 31 '20 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.