0

Hi i have a problem cuz I'm trying do sth like below: this method has adnotation AuraEnabled

Contact contact = getContact((Id)contactId);
contact.Inactive__c = true;

User user = getUserByContactId(contactId);
user.IsActive = true;
update user;
update contact;

And I'm receiving error like : MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Contact: [] ... what should i do ?

3

You can perform DML operations on more than one type of sObject in a single class using the following process:

  1. Create a method that performs a DML operation on one type of sObject.
  2. Create a second method that uses the future annotation to manipulate a second sObject type.

DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions. For example, you can’t update an account and a user role in a single transaction.

You can’t use the following sObjects with other sObjects when performing DML operations in the same transaction.

  • FieldPermissions
  • Group
  • GroupMember

You can only insert and update a group in a transaction with other sObjects. Other DML operations aren’t allowed.

  • ObjectPermissions
  • PermissionSet
  • PermissionSetAssignment
  • QueueSObject
  • ObjectTerritory2AssignmentRule
  • ObjectTerritory2AssignmentRuleItem
  • RuleTerritory2Association
  • SetupEntityAccess
  • Territory2
  • Territory2Model
  • UserTerritory2Association

You can update a user in a transaction with other sObjects in Apex code saved using Salesforce API version 14.0 and earlier

You can update a user in a transaction with other sObjects in Apex code saved using Salesforce API version 15.0 and later if the user isn’t included in an Lightning Sync configuration (either active or inactive) and the following fields are not updated:

  • UserRoleId
  • IsActive
  • ForecastEnabled
  • IsPortalEnabled
  • Username
  • ProfileId

More information here:-

  1. Error 'Mixed DML Operation' when you create or update records through Process Builder or validation rule
  2. sObjects That Cannot Be Used Together in DML Operations
  3. DML operations on sObjects results in MIXED_DML_OPERATION Error
| improve this answer | |
3

This is a salesforce limitation and the solution is to keep the non setup object (Contact) in the synchronous call, move the setup object (User) in async call either with a future or queueable method.

Refer the documentation for more details: SObjects That Cannot Be Used Together in DML Operations that also has an example.

Article to avoid Mixed DML Operations in Test Methods is also worth reading.

| improve this answer | |

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.