Most of us received such error "MIXED DML OPERATION"
while running test classes.
What is
Mixed DML Error?
DML operations on certain
sObjects can’t be mixed with other sObjects in the same transaction. This is
because some sObjects affect the user’s access to records in the organization.
These types of sObjects must be inserted or updated 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, there are no restrictions on delete DML operations.
Source: Mixed
DML
How to avoid
in your Test class?
Here is the syntax to avoid test class errors in Mixed DML
Operation salesforce. If you are inserting Setup and Non-Setup object means use
System.runAs() and pass the current user information, you will get rid of from
the Mixed DML errors.
- @isTest
- public class ShowEmailUtilsTest
- {
- Public static testmethod void Test_ShowEmailUtils()
- {
- User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
- System.runAs (thisUser)
- {
- // Add your test class content here
- }
- }
- }
No comments:
Post a Comment