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;
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.
To resolve the error when enabling a contact manually:
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:
UserRole record (for example, the 'CEO' role)System.runAs(adminUser) to ensure the code executes with the required Role contextThis 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;
}
Error "Portal Account Owner has No Role" on Customer User Activation
Create Experience Cloud Site Users
Object Reference - User
000385901

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.