Loading

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

Дата публикации: Jun 27, 2024
Описание

You may see the error "UNKNOWN_EXCEPTION, portal account owner must have a role: []" when trying to enable a contact as a partner or customer portal user.

Following snippet of code might result in the above error. 
  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;
}
Номер статьи базы знаний

000385901

 
Загрузка
Salesforce Help | Article