+ Start a Discussion
King KooKing Koo 

Trying to understand Mixed DML operations.

Hi there

There  are many posts on this forum already on how to fix Mixed DML Operations.

I am not asking for how to solve this.  Obviously using the @future method is one way to do it, if not the only.

I'm hoping for someone to explain to me why is that a problem?  Why can I not create an account and a user within the same transaction?  The documentation says it'll affect the access to records in the org.  However, I don't think I understand what that means.

I have already looked at a lot of the official documentation, so instead of sending URLs to official documentation, I hope someone can explain that to me in plain English.

Thank you very much!
King
Kevin CrossKevin Cross
My understanding of the issue is it because objects like User are Setup Objects (https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/reference_objects_setup.htm).  Since setup objects affect metadata, their creation happen in the setup area of Salesforce.  When you create non-setup objects (e.g., Accounts, et cetera), they run in the normal user context.  Therefore, the "problem" is the context switching synchronously.  My guess is @future running asynchronously is the reason it is a solution.
Kevin CrossKevin Cross
In more plain English: If I am on the Account page then click Setup, I no longer have access to the Accounts tab information and vice versa.  It is trying to press a button in two rooms at the same time with a locked door in between.  One of them has to happen later. *smile*