Reading the documentation here, the advice is to run certain DML operations within a System.runAs(user) block.

I don't really understand why that needs to happen. I know that it works - as my unit test for creating an EmailTemplate is now working. But I don't really know why it has to be within a specified user context.

Can someone provide a clearer insight than the documentation as to why this needs to happen?

1 Answer 1


Follow the link to sObjects That Cannot Be Used Together in DML Operations to better understand (emphasis mine):

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.

The key with using system.runAs is that you create a separate transaction, and that enables you to work with both types of sObject.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .