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
Adriana VoyceAdriana Voyce 

MIXED_DML_OPERATION... System.runAs

I created a trigger, and it works great in Sandbox, but when I launched it to production I receieved the following error:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger autostaff caused an unexpected exception, contact your administrator: autostaff: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Staff__c, original object: User: []: Trigger.autostaff: line 19, column 1 

My trigger is written as follows:If creates a record on the staff object when a user is created 

trigger autostaff on User (after insert) {

List <Staff__c> newstaff = new list <Staff__c> ();

for (User U : Trigger.new){

Staff__c Staff = new Staff__c();

Staff.Employee__c = U.ID;



Staff.Name      = U.Full_Name__c;

newstaff.add(Staff);

}

insert newStaff;

}

From what I have read I need a to add System.runAs as part of my code, but I am not sure how that should be writttten...

Any help is appreaciated.... Please and thank you :) 

 
Brian FordBrian Ford
I found a pretty good explanation with an example on the forum
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000Ah6dIAC
Deepak Kumar ShyoranDeepak Kumar Shyoran
This error Occur when you tried to performed DML operation on Custom and Standard Object  in single thread. You can avoid this error by splitting thread put your custom or standard DML inside @future method.
For more help visit https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects.htm