error

System.DmlException: MIXED_DML_OPERATION

What does this error mean?

This error occurs when Apex performs DML operations on both setup objects and non-setup objects in the same transaction.

Setup objects affect system configuration or security access (like User, PermissionSet, UserRole), while non-setup objects store business data like Account, Contact, or Opportunity.

Salesforce blocks this because modifying configuration and data together may cause inconsistent access permissions. [oai_citation:0‡Developer](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm?utm_source=chatgpt.com)

Common Causes

1. Creating User and Account in same transaction

Inserting a User record while inserting or updating business records.

2. Assigning Permission Sets in triggers

Triggers assigning PermissionSetAssignment while processing records.

3. Automation mixing setup objects

Flows or Apex performing DML on both User and Account records together.

How to Fix It

Solution 1 — Use @future method

Apex

Account acc = new Account(Name='Test Account');
insert acc;

// move setup object DML to async
UserHelper.createUserAsync();

Solution 2 — Queueable Apex

Apex

System.enqueueJob(new CreateUserJob());
lightbulb

Pro Tip: Use Queueable Apex instead of @future when you need chaining or job monitoring.