Development questions

How do Mixed DML Errors happen in triggers (e.g., creating a User and an Account in the same transaction) and how do you resolve them?

DevelopmentMediumDeloittePwCAccenture

Mixed DML error (MIXED_DML_OPERATION) occurs when setup objects (User, Group, GroupMember, PermissionSetAssignment, UserRole, etc.) and non-setup objects (Account, Contact, custom objects) are modified in the same transaction.

Solutions:

  • Move the setup-object DML (or the non-setup one) to a separate transaction using @future or Queueable Apex.
  • In tests, wrap one side in System.runAs().
apex
@future
public static void assignPermSet(Set<Id> userIds) { /* insert PermissionSetAssignment */ }
apex-triggertriggermixed-dmlasync