“Mixed DML Operation” Exception
Osama | August 21, 2010I am writing after quite a long time. Was busy at work and studies as well.
There is a very annoying exception “MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): {sObject} original object: {sObject}”
There you cannot perform DML on what salesforce calls setup objects(User in this case) and non-setup object in the same context.
There is a workaround that allows multiple DML operations in same transactions
If you want to perform two DML operations sequentially, create a separate method for 2nd DML operation with @future token.
make sure you put System.RunAs() in the method. Your overall method should look like this
private static void myFunc()
{
///1st DML operation
User usr = [Select id from User where Id = :UserInfo.getUserId()];
System.RunAs(usr)
{
Test.startTest();
myFunc2();
Test.stopTest();
}
}@future
private static void myFunc2()
{
///2nd DML operation
}
I hope this helps










