You need to sign in to do that
Don't have an account?

MIXED_DML_OPERATION, DML operation on setup object is not permitted Error
I'm trying to copy a new user as contact by using a trigger but I'm getting the following error
MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Contact, original object: User
trigger Acceleration_User_Copyto_Contact on User (after insert, after update) { // Map<String,User> newLead = new Map <String, User>(); system.debug('Acceleration_User_Copyto_Contact Trigger....'); for ( User user : System.Trigger.new) { if (user.Email.contains('acceleration')) { system.debug('Acceleration_User_Copyto_Contact Trigger..2.'); Integer RecordCount = [select count() from Contact c where c.Email = : user.Email]; system.debug('Acceleration_User_Copyto_Contact Trigger..2a .' + RecordCount); if (RecordCount == 0) { String AccountId = ''; for ( Account a : [Select a.Id From Account a where Name = 'Acceleration']) { system.debug('Acceleration_User_Copyto_Contact Trigger..3.'); AccountId = a.Id; Contact con = New Contact(); con.AccountId = AccountId ; con.Email = user.Email; con.Firstname = User.Firstname; con.Lastname = User.Lastname ; con.User__c = User.Id; insert con; } } } } }
Hey
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 can be found here although I still had some issues. Let me know if the posted solution works for you, if not mail me and I'll send you mine.
Cheers,
Wes
All Answers
Hey
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 can be found here although I still had some issues. Let me know if the posted solution works for you, if not mail me and I'll send you mine.
Cheers,
Wes
I have just run into the same issue when attempting to update the User object, as well as a custom object in the same Apex class. Can you please educate me how you got around this. I am not understanding the other contributor's workaround.
Thanks you!
Can you please send me the solution to pandeyhm@gmail.com. I have been dealing with the same issue
is it resolved?
i dont get what this bug means
im not performing two dml operations sequentially
and it only pops up when i try to set the userRoleId of a test user
this is happening in my test class where i simulate a manager approving something
im creating an account and a case and having defualt user call up a page
i then create a test user and try to call up the same page
but when i set the users role id, i get this error
if i dont set the role, no error
not sure if bug...
heres some code for y'all
error happens on the 'insert u;' line
I think this issue is resolved now. I am creating a user from a contact trigger and after that I am making an update to the contact. and it seems to be working fine. It seems mixed dml is allowed now. not sure when this change was made - may bespring 13?
I'm still getting this error with Summer '13. My test class creates a user, then creates an account. Strangely, the test runs just fine in the Force.com IDE but gets the MIXED_DML_OPERATION error in the standard UI. As far as I can tell I'm using best practices for unit testing - creating all my test data on the fly, including a user that my test needs to be there. So why is this happening, and what is the current best workaround?
I've never encountered this before, so it seems like maybe the bug had been fixed but was re-introduced in Summer '13.
I am gettiing this error in a trigger that updates a user and then attempts to update a custom object. I'll be using the @future to get around it, but it's not beautiful because @future methods only take primitives, and i need to update a list of objects. ehhhhh...
Hi all ,
its not fixed in any of the Salesforce release .The thing is that ,there is concept called as setup and non-setup objects e.d USer and contact .
You cant perform the two DML operations on these objects in the same context .Only workaround is using @future method in another class. But remember there is batch class running on any of the setup and non setup objects ,the batch class will fails .
Only workaround is use of @future method .
Regards,
Ashish
For the previously created custom non-setup object User_Access_Privileges__c that links back to User using the lookup field Related_User__c (use Create->Object->New to create your own):
The integration class containing the @future method looks like this (create using Develop->Apex Classes->New) It is referenced from the following User Trigger UserAccessAdd as follows:
Note: TeamTrackingLogUpdates is a shared class which allows batch jobs to turn off this trigger because this trigger will cause issues if run in batch mode.
And here's the TeamTrackingLogUpdates shared class just for good measure:
Testing Hint: Use Test.startTest() before adding/updating the setup object and Test.stopTest() after before your system.Assert tests to ensure the @future has executed before testing.
Instead of Insert or update call use Database.Insert or Database.update inside try-catch and that should solve your problem.
System.DmlException: Update failed. First exception on row 0 with id 00318000008sC90AAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SyncProductstoUser: execution of AfterUpdate
caused by: System.DmlException: Update failed. First exception on row 0 with id 00518000000MvpVAAS; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Account: []
Trigger.SyncProductstoUser: line 65, column 1: []
Working Solution
Trigger:
trigger SyncProductstoUser on Contact (after update)
{
Set<Id> UserIds = new Set<Id>();
Set<Id> ConIds = new Set<Id>();
for (Contact contactInLoop : Trigger.new)
{
if(contactInLoop.Id != null && contactInLoop.Id != null)
{
ConIds.add(contactInLoop.Id);
}
}
List<User> Users = [Select Id,Product__c, Product_Access__c, ContactId from User where ContactId =: ConIds];
Map<Id, User> UserContactMap = new Map<Id, User>();
// loop through all retrieved Contacts
for(User u : Users)
{
// get all Contacts for the User
UserContactMap.put(u.ContactId, u); // put the User Contacts in a map by Contact Id
}
for (Contact contactInLoop : Trigger.new)
{
if (contactInLoop.Id != null && contactInLoop.Product_Access__c != null && contactInLoop.Product__c != null )
{
User ur = UserContactMap.get(contactInLoop.Id);
if(ur != null)
{
ur.Product_Access__c = contactInLoop.Product_Access__c;
ur.Product__c = contactInLoop.Product__c;
update ur;
}
}
}
}
Test Class
@isTest(SeeAllData = True)
public class TestSyncProductstoUser
{
static testMethod void UpdateContact()
{
Account Acc = new Account(Name='ABC CORPORATION');
insert Acc;
Contact Con = new Contact(AccountId=Acc.Id,FirstName='ABC',LastName='User1',Email='ABC1User1@ABC.com',Product__c='ABC', Product_Access__c='ABC User');
insert Con;
User userToCreate = new User();
String ProfileId=[Select Id,Name from Profile where Name='ABC Community'].Id;
userToCreate.FirstName = Con.FirstName;
userToCreate.LastName = Con.LastName;
userToCreate.Email = Con.Email;
userToCreate.Username = 'ABC@community.com';
userToCreate.Alias = 'ABC1';
userToCreate.ProfileId = ProfileId;
userToCreate.ContactId = Con.Id;
userToCreate.Product__c = Con.Product__c;
userToCreate.Product_Access__c = Con.Product_Access__c;
userToCreate.TimeZoneSidKey = 'America/Denver';
userToCreate.LocaleSidKey = 'en_US';
userToCreate.EmailEncodingKey = 'UTF-8';
userToCreate.LanguageLocaleKey = 'en_US';
insert userToCreate;
system.debug('***Contact ID' + Con.Id);
System.runAs(userToCreate)
{
Con.Product__c = 'XYZ';
Con.Product_Access__c = 'XYZ User';
update Con;
}
}
}
SFDC's solution:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects_test_methods.htm
The solution that worked for me was to do the creation of those User/Group test data records using System.runAs.
I had the same problem, and this is the solution:
public void afterUpdate(map<id, User> oldUsers, List<User> newUsers){
list<id> activeContactIds = new list<id>();
list<id> unactiveContactIds = new list<id>();
for (User u : newUsers){
if (u.isActive && !oldUsers.get(u.id).isActive)
activeContactIds.add(u.ContactId);
if (!u.isActive && oldUsers.get(u.id).isActive)
unactiveContactIds.add(u.ContactId);
}
UpdateActiveContacts(activeContactIds, true);
UpdateActiveContacts(unactiveContactIds, false);
}
@future
public static void UpdateActiveContacts(list<id> contactIds, boolean active){
list<Contact> contacts = new list<Contact>();
for (id i : contactIds){
Contact con = new Contact(id = i, Portal_User__c = active);
contacts.add(con);
}
if (contacts.size() > 0)
update contacts;
}
(This does not answer the question but asserts that the issue is back).
This was previously (maybe last year) not an issue.
We had implemented the below commented code with no issue.
The commented code was working just fine until a recent release (this year).
A new deployment to Prod has uncovered this is now broken again (In Winter 17)
So it looks like Salesforce has regressed this issue back into prod.
NB: our issue appeared in a Test Method
That I need to create a User record on insert of custom object,as well as if a error occurs in creation of User than that error should also update in custom object.
What we are doing is in csutom object after insert tirgger we are creating User in future method.If the User is created it is good but if not we need to capture that error and show it in our custom object.
At this point when we need to show the error,this exception is thrown
"MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa)"
@Ankit Maini.
Try a little bit of this:
https://salesforce.stackexchange.com/questions/75779/future-method-calling-another-future-method.
Obvious issue you have is you cannot chain future methods together.
If you can stack your two future methods in order - You can then put the Custom Id into the User Record. Then in the second Future Method you can query to see if the User Record was created (users.size() >0 ) If not update the custom record.
However this will need them to be processed in order and I do not think @Future alone will do this.
Problem with this will be that you may not be able to post the exception message back into the custom object.
Try this
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm
Lastly, a Salesforce to Salesforce Integration with a tool like Hubofy.com
Realtime callout to Hubofy and then Hubofy manages the rest of the process for you, does 1 or 2 API calls back into your Salesforce.
apex_queueing_jobs worked for me :)
Thanks
Thanks @Jamie Browning
Thanks @R Draper!
Literally queried myself as a user record in my @testSetup, "ran as" myself, and it let me insert my holiday :)
No @future needed
smadav 2021 (https://www.smadav2021.net/smadav-2021/), Smadav 2021 (https://www.smadav-2021.com/smadav-2021/), Smadav 2021 (https://www.smadavsecurity.com/smadav-2021.html), Smadav 2021 (https://www.smadavdownloads.com/smadav-2021/), Smadav 2021 (https://www.antiviruspedia.net/smadav-2021/)
Smadav 2021 (https://www.smadav-2021.net/smadav-2021-free-download/)
Smadav 2021 (https://antivirusupdate.net/smadav-antivirus-2021-new-version-free-download/)
fujitsu scansnap ix1500 driver (https://www.fujitsuscansnap.net/fujitsu-scansnap-ix1500-software-download/)
fujitsu scansnap ix1500 driver (https://www.fujitsuscansnapdriver.com/fujitsu-scansnap-ix1500-software-download/)
fujitsu scansnap s500m driver (https://www.scansnapfujitsu.net/fujitsu-scansnap-s500m-driver-download/)
smadav 2021 (https://www.smadav2019.com/smadav-2021-free-download.html)
smadav 2021 (https://www.smadav2020.net/smadav-2021-free-download/)
Below is the code.. Please let me know if i miss to apply anything..
public class TestMixedDmlError {
public static void function1(){
Indeed_com_AppForm__c i;
User u = [select id, name, CompanyName from user where name='UserName1'];
if(u.CompanyName == null){
u.CompanyName = u.Name + ' ' + 'Company';
i = new Indeed_com_AppForm__c();
i.Name = 'Created from TestMixedDmlError Apex Class';
insert i;
update u;
}
system.debug(u.CompanyName);
system.debug(i.name);
}
}
Smadav 2021 (https://www.softwareanddriver.com/2019/12/smadav-2021-antivirus-free-download.html" target="_blank) | Smadav 2021 (https://www.freedownloaden.com/2019/12/smadav-2021-antivirus-free-download.html" target="_blank) | Smadav 2021 (https://www.sourcedrivers.com/2019/12/smadav-2021-antivirus-free-download.html" target="_blank) | Smadav 2021 (https://www.smadav2021.com/2019/08/smadav-2021-pro-free-download.html" target="_blank) | Smadav 2021 (https://www.avastech.com/2019/12/smadav-2021-antivirus-free-download.html" target="_blank) | Smadav 2021 (https://www.avastupdates.com/2019/12/smadav-2021-antivirus-free-download.html" target="_blank) | 2020 Mercedes-Benz (https://www.carshighlight.com/2019/07/2020-mercedes-amg-cla45-review-specs.html" target="_blank) |
Smadav has an advantage with its little installer size (under 10 MB) and low use of the web while dynamic in your PC. And furthermore, Smadav just utilizing little part of your PC assets. Smadav more often than not just utilizes little memory and little CPU use. With this little asset utilization, Smadav won't vigorously moderate your PC. Furthermore, you can even now introduce another antivirus that will together work with Smadav to ensure your PC.