How do you handle Mix DML errors when creating Users & Accounts in the same transaction?

In Salesforce, Mix DML error happens when you try to perform DML on setup objects (like User, Profile, PermissionSetAssignment, etc.) and non-setup objects (like Account, Opportunity, etc.) in the same transaction.

So if you create a User and an Account in the same transaction, you’ll hit a MIXED_DML_OPERATION error.

Why the Mixed DML Error Occurs?

Salesforce doesn’t let you update users and regular records (like accounts or contacts) in the same transaction. This is because user changes affect org-wide settings, so they must be handled separately.

Ways to Handle Mix DML Errors

1 Use @future Method

    Put the non-setup DML (e.g., Account) in a @future method and commit it in a separate transaction.

    public class UserAccountHandler {
        public static void createUserAndAccount() {
            // Create User (Setup object)
            User u = new User(
                FirstName = 'Test',
                LastName  = 'User',
                Email     = 'testuser@example.com',
                Username  = 'testuser@example.com',
                Alias     = 'tuser',
                CommunityNickname = 'testuser',
                TimeZoneSidKey    = 'Asia/Kolkata',
                LocaleSidKey      = 'en_US',
                EmailEncodingKey  = 'UTF-8',
                ProfileId         = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id,
                LanguageLocaleKey = 'en_US'
            );
            insert u;
            
            // Call future method for Account
            createAccountFuture(u.Id);
        }
    
        @future
        public static void createAccountFuture(Id userId) {
            Account acc = new Account(Name = 'Test Account');
            insert acc;
        }
    }
    

    2 Use Queueable Apex

    Create User & Account using Queueable

    public class UserAccountService {
        public static void createUserAndAccount() {
            // Create User (Setup object)
            User u = new User(
                FirstName = 'Test',
                LastName  = 'Queueable',
                Email     = 'queueuser@example.com',
                Username  = 'queueuser@example.com',
                Alias     = 'quser',
                CommunityNickname = 'queueuser',
                TimeZoneSidKey    = 'Asia/Kolkata',
                LocaleSidKey      = 'en_US',
                EmailEncodingKey  = 'UTF-8',
                ProfileId         = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id,
                LanguageLocaleKey = 'en_US'
            );
            insert u;
    
            // After user creation → enqueue job to create Account
            System.enqueueJob(new AccountCreatorQueueable(u.Id));
        }
    }
    
    public class AccountCreatorQueueable implements Queueable {
        private Id userId;
        
        public AccountCreatorQueueable(Id uId) {
            this.userId = uId;
        }
        
        public void execute(QueueableContext context) {
            // Create an Account linked logically to the User
            Account acc = new Account(
                Name = 'Account for User ' + userId
            );
            insert acc;
        }
    }
    
    

    // Just call this method from somewhere (e.g., trigger, LWC controller, etc.)
    UserAccountService.createUserAndAccount();

    3 Platform Events (Advanced)

    • Publish a Platform Event after User creation.
    • A subscriber triggers asynchronously and inserts Accounts.

    Handling Mix DML errors in Salesforce is all about separating setup and non-setup object operations into different transactions. Using approaches like Queueable Apex, @future methods, or even Platform Events ensures your code stays clean, scalable, and error-free. In most real-world projects, Queueable Apex is the go-to solution for keeping logic asynchronous yet controlled.

    By following this pattern, you not only avoid Mix DML pitfalls but also design a solution that’s robust and future-ready.