function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yogesh Malbhage 18Yogesh Malbhage 18 

portal account owner must have a role: [] exception is Salesforce

While inserting an user we are getting an exception mentioned above.
Please provide a solution ASAP as it is very ritical.
Thanks in advance!!!!
Khan AnasKhan Anas (Salesforce Developers) 
Hi Yogesh,

Greetings to you!

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
  }

Please refer to the below links which might help you further.

https://help.salesforce.com/articleView?id=000175522&type=1

https://www.xgeek.net/salesforce/to-fix-portal-account-owner-must-have-a-role-error-in-salesforce-community-unit-test/

http://www.fishofprey.com/2015/08/salesforce-unknownexception-portal.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Yogesh Malbhage 18Yogesh Malbhage 18
Hi Anas,

Thanks for your quick response.

My case is simila to the solution you provided as https://help.salesforce.com/articleView?id=000175522&type=1

The solution says i should go to user edit page and assign a role.

But my question is how can I go to user edit page as user itself has not created due to an eception mentioed above. Please help me through this.
Khan AnasKhan Anas (Salesforce Developers) 
The admin user is the one inserting the account in your test. Add a role to your admin user before attempting to run the test.

Please refer to below links with a similar discussion which might help you further.

https://developer.salesforce.com/forums/?id=906F0000000BRdEIAW

https://developer.salesforce.com/forums/?id=9060G0000005dRHQAY

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Yogesh Malbhage 18Yogesh Malbhage 18
Hi Anas,

We are using the following code.
User portalUser = new User(
                                       contactId = con.Id, 
		 			username = con.Email, 
                                        firstname= con.FirstName,
                                        lastname= con.LastName, 
                                        email= con.Email,
                                        communityNickname = con.LastName + '_' + Math.random(),
                                        alias = alias, 
                                        profileid = profile.Id, 
                                        emailencodingkey='UTF-8',
                                        languagelocalekey='en_US', 
                                        localesidkey='en_US', 
                                        timezonesidkey='America/Panama',
                                      	isActive = false
                                      );
            
            insert portalUser;

can you please tell me what is wrong with this code? It is same as in the solution provided above. but still I am getting an error since account owner of the related contact is not having role.