I am trying to create a community user in an after update trigger of account. I am setting the UserRoleId = null but still am getting MIXED_DML_OPERATION exception. As per documentation:

You can insert a user in a transaction with other sObjects in Apex code saved using Salesforce API version 15.0 and later if UserRoleId is specified as null.

Here is my simple method that return a User object:

public class UtilClass {
        * Created by : Sarang D
        * Description: Method to create user.
    public static User createUser(String profileId, String federationId, String firstname, String lastName,
                                     String alias, String email, String username, String communityNickname, Id contactId, String TimeZoneSidKey,
                                    String EmailEncodingKey, String LanguageLocaleKey, String LocaleSidKey, Id UserRoleId){
        return new User(
        LastName = lastName,
        Alias = alias,
        Email = email,
        Username = username,
        ProfileId = profileId,
        communityNickname = communityNickname,
        contactId = contactId,
        TimeZoneSidKey = TimeZoneSidKey,
        EmailEncodingKey = EmailEncodingKey,
        LanguageLocaleKey = LanguageLocaleKey,
        LocaleSidKey = LocaleSidKey,
        FederationIdentifier = federationId,
        UserRoleId = UserRoleId);

And calling this from an after update trigger in a for loop:

List<User> communityUserListTOBeCreated = new List<User>();
for(Account_Relationship__c acRelationship : []){      // Somme query
    //Passing data here, specifically setting UserRoleId = null.
insert communityUserListTOBeCreated ;  //Throws exception here.
  • Are you creating the contact record first as that is required for a community user? I don't see where you are passing a Null value to the UserRoleId from the code you have included in the question.
    – Dave Humm
    Commented Oct 9, 2020 at 9:31
  • Yes, my contact record is already created and I am passing contactId as well. Did not mention all the fields to keep the questions short...I am passing UserRoleId to method createUser
    – Sarang
    Commented Oct 9, 2020 at 9:34
  • Do you also add Permission Set Assignments?
    – ytiq
    Commented Oct 9, 2020 at 12:48
  • no didn't add permission set.
    – Sarang
    Commented Oct 9, 2020 at 13:06

1 Answer 1


There was a workflow rule on user object that was updating the user object and since it is called from account trigger transaction, and updation of user is not allowed in same transaction as standard objects, this error is produced.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .