i creating customer portal user using apex i got this issue as give below.

Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: []

i already selected role for Admin User but still i am getting this exception. any one help i would really appricate.


Its not related to Admin User Role .Its the Account for which contact(User in portal concept) are created the Role is Mandatory.The Account owner may not be always your user record .

  • Thanks for your reply Mohith Kumar. here i am using Force.com Site url for call this serivce . Account is created by force.com site user. how can create role for force.com site user, there is no option for add role to force.com site user. any help
    – kittu
    Aug 21 '13 at 8:54
  • once lead is convert then account created by Force.com site user. here Account owner is Force.com site user this user not have role. that's why i am getting this exception Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: [] how to solve this problem...
    – kittu
    Aug 21 '13 at 12:37
  • Write a trigger on lead conversion to change the owner of Account. Aug 23 '13 at 1:16

This error is generated because the user was not assigned 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:

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 Profile'].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@appirio.com';   
  user.CommunityNickname = 'testUser123';
  user.Alias = 't1';
  user.Email = 'no@email.com';
  user.IsActive = true;
  user.ContactId = c.Id;

  insert user;

  System.RunAs(user) {
    // do all of my tests
  • 1
    This answer is also found at help.salesforce.com/…
    – Legolas
    Apr 23 '15 at 7:54
  • How does this work, no-where in this code is the user record assigned a role? user.UserRoleId ?? Jul 9 '15 at 18:33

I had the same problem and adding a role to the Admin User did not fix the issue. My test code validated after I added a role to MY OWN profile.

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.