Newer Version Available
Mixed DML Operations in Test Methods
Test methods allow for performing mixed Data Manipulation Language (DML) operations
that include both setup sObjects and other sObjects if the code that performs the DML
operations is enclosed within System.runAs method
blocks. You can also perform DML in an asynchronous job that your test method calls. These
techniques enable you, for example, to create a user with a role and other sObjects in the
same test.
The setup sObjects are listed in sObjects That Cannot Be Used Together in DML Operations.
Example: Mixed DML Operations in System.runAs Blocks
This example shows how to enclose mixed DML operations within System.runAs blocks to avoid the mixed DML
error. The System.runAs block runs in the
current user’s context. It creates a test user with a role and a test
account, which is a mixed DML
operation.
Use @future to Bypass the Mixed DML Error in a Test Method
Mixed DML operations within a single transaction aren’t allowed. You can’t perform
DML on a setup sObject and another sObject in the same transaction. However, you can
perform one type of DML as part of an asynchronous job and the others in other
asynchronous jobs or in the original transaction. This class contains an @future method to be called by the class in the
subsequent
example.
This class calls the method in the previous
class.