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;
This error is generated because the user who running the code does not have a role.
Ensure that the user running the code below has a role assigned (eg. Sys Admin user does not have a role assigned by default). To resolve the error, assign the User a Role.
You will also see this same error (for the same reason) when trying to enable a contact as a partner or customer portal user.
The following sample code demonstrates how to avoid this error in a Test class:
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.