function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BhawnaBhawna 

error: MIXED_DML_OPERATION

I am having the following error in my unit tests. I was unable to find any information on this error in the documentation.
This error is generated after the trigger is deployed and seems that we cannot update User record after any other Custom object Record's update in trigger in a transaction.
FYI, the trigger is for after insert/update/delete on User's custom field and is deployed successfully.

error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object

Can any one help me out from this. I'll be very thankful :smileyhappy:.
BhawnaBhawna
Hi,
     I would like to explain my problem again :
I have created a trigger to Update the User's Custom Field from an Opportunity form.
Means there are 2 tables : Opportunity and User, want to update the custom field of User from Opportunity custom field.
The trigger and the test class are deployed successfully in the Sandbox "version Discover Winter '09" but it throws an DML error when I update the user field in the Opportunity Form.
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Opportunity_User: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0 with id 00530000000qyKVAAY; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object: User: [Id]
Can any one guide me where I am wrong or we can't do this.

Vijay RautVijay Raut
Hi Bhawna,

Post API Version 13.0 we cannot make operation on setup and non-setup objects in single context.

e.g. We cannot insert/update Account, Opportunity, Custom Object (non-setup objects) and then insert / update User, Profile, Group (setup Objects).

In your case, you have two options.

1. Try to change your API Version to 12.0 or below. As this restriction is enforced only API version 13.0 and above.

2. You may use Future annotation (which creates new context from current context) in which you can update your User. I mean move part of your code which update User in Future method.

Hope this would help.

Cheers,
V.R.
BhawnaBhawna

Error:Apex trigger Opportunity_User caused an unexpected exception, contact your administrator: Opportunity_User: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You cannot update a customizable setup entity, except when running tests: Trigger.Opportunity_User: line 31, column 9

 

This is new error i am getting now.

 

also, can you elaborate more on using FUTURE method in triggers?

osamanosaman

There is a work around for that.

If you want to perform two DML operations sequentially, create a seprate 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

}