1

I've been trying to update user emails and profiles after a sandbox is created and using the SandboxPostCopy interface. However I keep running into the below error:

   Update failed. First exception on row 0 with id xxxxxxxxxxxx; 
    first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted
    after you have updated a non-setup object (or vice versa):

I know that I have to have a future job to get around this but has anyone successfully managed to not use an asynchronous job?

  • 1
    What else are you doing in your SandboxPostCopy? I usually utilize a queuable class to handle my user updates related to this, as it's my last step and I don't need it to be done in any specific timing/order. As mentioned, you can use a future method to handle it async as well as long as you don't depend on those updates to finish to do downstream logic in the sandbox refresh script. – Kris Goncalves Mar 9 at 12:29
  • Thanks for your reply, I'm updating just custom settings other than user, so I don't see why I'm getting the Mixed DML error. – Minz Mar 9 at 15:29
1

The Apex Developer Guide has a list of the sObjects that cannot be used together in DML Operations that is useful.

As you can see, Custom Setting and User is on there which is why you run into the issue.

There's also a help document from Salesforce regarding this error message that lets you know the issue is when there's multiple updates on the listed sobjects in the same transaction

If the user update is not something you need to wait to finish for to do other logic, you can simply move that code into its own @future method which would perform the update in an asynchronous update (not the same transaction) which would avoid the exception.

public without sharing class SandboxRefreshScript implements SandboxPostCopy {
    public void runApexClass(SandboxContext context) {
        //move your user activation to another method
        activateUsers();
    }


    @Future
    private static void activateUsers() {
        //pull your users and perform the update here
    }
}
| improve this answer | |
  • Thanks! makes complete sense now :D – Minz Mar 9 at 16:13

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.