Newer Version Available

This content describes an older version of this product. View Latest

sObjects That Cannot Be Used Together in DML Operations

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. However, deleting a DML operation has no restrictions.
You can’t use the following sObjects with other sObjects when performing DML operations in the same transaction.
  • FieldPermissions
  • Group

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

  • GroupMember

    You can insert and update a group member only in a transaction with other sObjects in Apex code saved using Salesforce API version 14.0 and earlier.

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

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

    You can insert a user in a transaction with other sObjects in Apex code saved using Salesforce API version 15.0 and later if UserRoleId is specified as null.

    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 following fields are not also updated:
    • UserRoleId
    • IsActive
    • ForecastEnabled
    • IsPortalEnabled
    • Username
    • ProfileId
  • UserRole
  • UserTerritory
  • Territory
  • Custom settings in Apex code saved using Salesforce API version 17.0 and earlier.

If you're using a Visualforce page with a custom controller, you can't mix sObject types with any of these special sObjects within a single request or action. However, you can perform DML operations on these different types of sObjects in subsequent requests. For example, you can create an account with a save button, and then create a user with a non-null role with a submit button.

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.

This process is demonstrated in the example in the next section.

Example: Using a Future Method to Perform Mixed DML Operations

This example shows how to perform mixed DML operations by using a future method to perform a DML operation on the User object.

1public class MixedDMLFuture {
2    public static void useFutureMethod() {
3        // First DML operation
4        Account a = new Account(Name='Acme');
5        insert a;
6        
7        // This next operation (insert a user with a role) 
8        // can't be mixed with the previous insert unless 
9        // it is within a future method. 
10        // Call future method to insert a user with a role.
11        Util.insertUserWithRole(
12            'mruiz@awcomputing.com', 'mruiz', 
13            'mruiz@awcomputing.com', 'Ruiz');        
14    }
15}
1public class Util {
2    @future
3    public static void insertUserWithRole(
4        String uname, String al, String em, String lname) {
5
6        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
7        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
8        // Create new user with a non-null user role ID 
9        User u = new User(alias = al, email=em, 
10            emailencodingkey='UTF-8', lastname=lname, 
11            languagelocalekey='en_US', 
12            localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
13            timezonesidkey='America/Los_Angeles', 
14            username=uname);
15        insert u;
16    }
17}