Loading

Error 'Portal account owner must have a role' when you enable an External User

Publiceringsdatum: Jun 22, 2026
Beskrivning

When attempting to enable a Contact as an Experience Cloud (formerly known as Community, Partner Portal, or Customer Portal) external user in Salesforce, you may encounter the following error:
"UNKNOWN_EXCEPTION, portal account owner must have a role: []"

This error occurs when the owner of the Account associated with the Contact does not have a Role assigned in Salesforce. System Administrator profiles do not have a Role assigned by default, so this error commonly appears when a System Administrator is the Account owner and attempts to enable portal access.
This error can occur both when running Apex code programmatically and when enabling a contact as a portal/Experience Cloud user manually through the Salesforce UI.

  Account a = new Account(Name='Test Account Name');
  insert a;

  Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
  insert c;

  User user = new User();
  user.ProfileID = [Select Id From Profile Where Name='Some Portal Profile Name'].id;
  user.EmailEncodingKey = 'ISO-8859-1';
  user.LanguageLocaleKey = 'en_US';
  user.TimeZoneSidKey = 'America/New_York';
  user.LocaleSidKey = 'en_US';
  user.FirstName = 'first';
  user.LastName = 'last';
  user.Username = 'test@uniquedomain.com';   
  user.CommunityNickname = 'testUser123';
  user.Alias = 't1';
  user.Email = 'no@email.com';
  user.IsActive = true;
  user.ContactId = c.Id;

  insert user;

Lösning

 

Root Cause

Salesforce requires that the Account owner has an assigned Role in the Salesforce Role Hierarchy when enabling portal or Experience Cloud users. Without a Role on the Account owner's User record, the portal user creation fails with the "portal account owner must have a role" error. This is a system requirement enforced at the platform level.

Manual Fix (UI)

To resolve the error when enabling a contact manually:

  1. Navigate to Setup > Users
  2. Find the Account owner's User record (e.g., the System Administrator user)
  3. Edit the user record and assign a Role from the Role field
  4. Save the changes, then retry enabling the portal or Experience Cloud user

Code Fix for Apex Test Classes

When writing Apex test classes that create portal or Experience Cloud users, use System.runAs() to execute the user creation as a user that has a Role assigned.
Approach:

  1. Query a UserRole record (for example, the 'CEO' role)
  2. Assign the queried role to an admin User record
  3. Update the admin User record
  4. Wrap the Account creation, Contact creation, and Portal User insertion inside System.runAs(adminUser) to ensure the code executes with the required Role context

This ensures that portal user creation in test classes does not fail due to the missing Role requirement.
(See Apex test class code example)

UserRole userrole = [Select Id, DeveloperName From UserRole Where DeveloperName = 'CEO' Limit 1];

User adminUser = [Select Id, UserRoleId From User Where Profile.Name='System Administrator' Limit 1];

adminUser.UserRoleId = userRole.Id;
update adminUser;

System.runAs(adminUser){
    Account a = new Account(Name='Test Account Name');
    insert a;

    Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
    insert c;

    User user = new User();
    user.ProfileID = [Select Id From Profile Where Name='Customer Community User'].id;
    user.EmailEncodingKey = 'ISO-8859-1';
    user.LanguageLocaleKey = 'en_US';
    user.TimeZoneSidKey = 'America/New_York';
    user.LocaleSidKey = 'en_US';
    user.FirstName = 'first';
    user.LastName = 'last';
    user.Username = 'test@uniquedomain.com';
    user.CommunityNickname = 'testUser123';
    user.Alias = 't1';
    user.Email = 'no@email.com';
    user.IsActive = true;
    user.ContactId = c.Id;

    insert user;
}
Knowledge-artikelnummer

000385901

 
Laddar
Salesforce Help | Article