You need to sign in to do that
Don't have an account?
Sam D.
Intermittent MIXED_DML_OPERATION error in Communities
I'm trying to figure out why we would only intermittently get a MIXED_DML_OPERATION error inside Communities, but not consistently. The code does update both User and Contact, but it works fine whenever we test it and most users have had no issue. However we just today got an error from one user that says:
Does anyone understand why this would only happen occasionally rather than consistently? If that's the restriction it seems like it would happen every time, which is doesn't.
Code follows. The error happened on "update theC" after having updated the user.
Update failed. First exception on row 0 with id <snip>; first 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: []
Does anyone understand why this would only happen occasionally rather than consistently? If that's the restriction it seems like it would happen every time, which is doesn't.
Code follows. The error happened on "update theC" after having updated the user.
public PageReference saveProfileInfo() { saveMsgProfileInfo = null; Boolean updated = false; if (aboutme != aboutme_previous) { System.debug('AboutMe is changed, updating user.'); u.AboutMe = aboutme; try { update u; updated = true; } catch (DMLException e) { log_error.logError(e); } } if (theC!= null && mystory != mystory_previous) { theC.My_Story__c = mystory; System.debug('My Story is changed, updating contact.'); try { update theC; updated = true; } catch (DMLException e) { log_error.logError(e); } } if (updated) { saveMsgProfileInfo = 'Saved.'; } else { saveMsgProfileInfo = 'Nothing saved.'; } return null; }
Hi,
Can you try doing all yoiur DML as the logged in use?
So it would be great if you (or anyone) could give a bit more detail about why you think doing it this way will help? And/or why I might be getting the intermittent error behavior where usually it updates User and Contact in the same transaction just fine, but apparently not always. Thanks!
https://developer.salesforce.com/forums?id=906F00000008wAZIAY
The best workaround I've seen is to apparently do the "update theC" in an @future context. IDK if that will work for you though. Maybe it would be best for you to switch it around and do the update of the User record in the @future context and do the update of the Contact within the page.
It appears (from what other users wrote) to mimic your situation; works sometimes, crashes in others.
This is what kmiller wrote in that same article thread;
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?
You're kind of lucky though - it's only one record, so you don't have to pass a list.... that would be bad news.
Putting one of the updates into a @future context is a good idea, though it sort of messes with being able to have a confirmation message that confirms that the changes were saved. But maybe it's better than occasionally triggering an error. Anyway thanks for the help.