When a contact is created I am trying to create community portal user. So first I wrote the user creation code in trigger itself but I got the error 'MIXED_DML_OPERATION'. So I moved my user creation code to a future method and my code is like below

Trigger handler Code

public void AfterInsert(List<Sobject> newMap){
        set<ID> conids = new set<ID>();
            for(Contact c : (List<Contact>) newMap){                
                if(Schema.getGlobalDescribe().get('Contact').getDescribe().getRecordTypeInfosById().get(c.RecordTypeId).getName().toUppercase() =='PROVIDERS'){
        catch(Exception Ex){

and here is my Future method class code

public class CreateUsers {
    public static void createCommunityUsers(set<Id> setContactids)
        List<contact> conlist = [select id,email,firstName,lastname from Contact where Id IN : setContactids];
         List<Profile> profileList = [Select Id from Profile where Name= 'Provider Customer Community Plus User' limit 1];
         List<UserRole> roleList = [Select Id from UserRole where Name= 'Customer Person Account' limit 1];

            List<user> usr = new List<user>();
            Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.EmailHeader.triggerUserEmail = true;       
                dmo.EmailHeader.triggerOtherEmail = true;
                dmo.EmailHeader.triggerAutoResponseEmail = true;       
            for(Contact con : conlist){              
                string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):''; nick += Datetime.now().getTime();       
                User newUser = new User(
                                alias = con.lastname,
                                email = con.email,
                                emailencodingkey = 'UTF-8',
                                firstname = con.firstName,
                                lastname = con.lastname,
                                languagelocalekey = 'en_US',
                                localesidkey = 'en_US',
                                contactId = con.Id,
                                timezonesidkey = 'America/Los_Angeles',
                                username = con.email+ Datetime.now().getTime(),
                                UserRoleId = roleList[0].Id,
                                CommunityNickname = nick,
                                ProfileId =profileList[0].Id,                           
                                IsActive = true);     
            Insert usr;

But I am always getting

FIELD_INTEGRITY_EXCEPTION, Invalid role assignment.: 

and if I remove this line of code

  UserRoleId = roleList[0].Id,

I am getting

UNKNOWN_EXCEPTION, portal account owner must have a role

Account record is also mapped properly to that contact Here is the debug trace

Contact:{Id=0033K00000GlwBJQAZ, [email protected], FirstName=Global, LastName=Test, AccountId=0013K00000kxIf7QAE}

User:{Alias=Test, [email protected], EmailEncodingKey=UTF-8, FirstName=Global, LastName=Test, LanguageLocaleKey=en_US, LocaleSidKey=en_US, ContactId=0033K00000GlwBJQAZ, TimeZoneSidKey=America/Los_Angeles, [email protected], CommunityNickname=solutions1632245022446, ProfileId=00e3K000000E0esQAC, UserRoleId=00E3K000000SRifUAG, IsActive=true}

1 Answer 1


The error says clearly whats needed

UNKNOWN_EXCEPTION, portal account owner must have a role

So what you need is to make sure the Account owner has a Role assigned and not your portal user.

  1. Find the Accounts of the Contacts records which you are creating experience cloud users.
  2. Find the owner of these Accounts and make sure they have a role assigned.
  • yes I checked that and I edited my question above and added the images
    – klnavi
    Commented Sep 21, 2021 at 18:26
  • Does your Account owner has the role? Who is the account owner? Commented Sep 21, 2021 at 18:30
  • 1
    Note not contact owner, it is Account owner that needs the role. Commented Sep 21, 2021 at 18:30
  • 1
    Thank you. I just checked the contact owner. Role is missing for account owner and things are looking good now.
    – klnavi
    Commented Sep 21, 2021 at 18:48

You must log in to answer this question.

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