You can perform DML operations on more than one type of sObject in a single class using the following process:
- Create a method that performs a DML operation on one type of sObject.
- 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.
You can only insert and update a group in a transaction with other sObjects. Other DML operations aren’t allowed.
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:
More information here:-
- Error 'Mixed DML Operation' when you create or update records through Process Builder or validation rule
- sObjects That Cannot Be Used Together in DML Operations
- DML operations on sObjects results in MIXED_DML_OPERATION Error