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
Sam D.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:

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;
		}


kevin Carotherskevin Carothers

Hi,

Can you try doing all yoiur DML as the logged in use?

public PageReference saveProfileInfo() {
    saveMsgProfileInfo = null;
    Boolean updated = false;
    User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
    System.runAs ( thisUser ) {
        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;
}


Sam D.Sam D.
Hi, thanks for the response. I can try that, though I won't really know if it works immediately since we only got this one error report.  

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!
kevin Carotherskevin Carothers
Read what Wes Nolte wrote about your error on this thread;

https://developer.salesforce.com/forums?id=906F00000008wAZIAY


Sam D.Sam D.
Thanks, saw that but it still doesn't explain why it seems to mostly work fine and only seems to have thrown the error intermittently.
kevin Carotherskevin Carothers
Looks like it's apparently not permissable to do update/inserts on setup and non-setup objects in the same context;   (according to Wes).

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.

Sam D.Sam D.
Ok Kevin, thanks for your responses.  I guess I'll have to settle for not quite understanding why the error only happens occasionally. 

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.