MIXED_DML_OPERATION

Setup objects and regular objects were modified in the same transaction. Setup objects include User, UserRole, Group, GroupMember, PermissionSet, PermissionSetAssignment and QueueSObject.

Common causes

  • Creating a user and a contact or account in one transaction.
  • A record trigger that assigns permission sets or adds group members.
  • A test method that inserts users and then test data.

How to fix

  • Move the setup object DML into a @future method or Queueable job:
@future
public static void assignPermissionSet(Id userId, Id permSetId) {
    insert new PermissionSetAssignment(
        AssigneeId = userId, PermissionSetId = permSetId);
}
  • In tests, wrap one of the two operations in System.runAs().
  • In flows, move the setup object change onto an asynchronous path.

You may also like...