Ask Search:
siva kumarsiva kumar 

Why the Salesforce introduced Mixed DML Error

Hi All,
As we know salesforce not supported any DML on Setup & Non Setup Objects during execution. Can anybody explain it cleary.

Regards,
VSK
Best Answer chosen by siva kumar
Raya BanerjeeRaya Banerjee
Hi Siva,

If we perform DML operation on standard/custom object and global objects(User, UserRole, Group, GroupMember, Permission Set, etc...) in same transaction this error will come.
To avoid this error, we should perform DML operation on standard/custom object records in a different transaction.
In general all the apex classes and apex triggers execute synchronously (execute immediately).
if we perform DML operation on standard/custom object records asynchronously (execute in future context), we can avoid MIXED-DML-OPERATION error.

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.

You can refer to this for more details
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm.

Thanks!
Mark it has correct Answer if t helps you,it may help to others even!

All Answers

Kishore B TKishore B T
This error occurs when you are performing DMLs on setup objects and Non-setup objects simultaneously. So in other words we can say in a piece of code you cannot INSERT/UPDATE/DELETE commands on Setup objects and Non-Setup objects together. Setup Objects are—User, RecordType, Profile etc. Non-Setup Objects are—All other general objects like native objects(Standard Objects) and custom objects falls in to category of Non-setup objects. Ex- Contact, Account, Lead etc.

Please look into the documentation 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm

+ To avoid it 
Please use @Future Annotation 
Raya BanerjeeRaya Banerjee
Hi Siva,

If we perform DML operation on standard/custom object and global objects(User, UserRole, Group, GroupMember, Permission Set, etc...) in same transaction this error will come.
To avoid this error, we should perform DML operation on standard/custom object records in a different transaction.
In general all the apex classes and apex triggers execute synchronously (execute immediately).
if we perform DML operation on standard/custom object records asynchronously (execute in future context), we can avoid MIXED-DML-OPERATION error.

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.

You can refer to this for more details
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm.

Thanks!
Mark it has correct Answer if t helps you,it may help to others even!
This was selected as the best answer
Pritam ShekhawatPritam Shekhawat
Take a look here for detailed explaination Fighting and fixing the “MIXED_DML_OPERATION” error !