Im getting above error when i was trying to run my test class. In my test class i will create an Account, Contact. And this contact id will be used in my original code to create Community user. I noticed following help article from SFDC. But couldn't figure out how to achieve. https://help.salesforce.com/HTViewSolution?id=000175522&language=en_US

Here they asked to create User on the Test class and Runas this user. But in my case, the community user will be created in the code. Please share any thoughts. Here is the detailed exception,

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, caused by: System.DmlException: Insert failed. First exception on row 11; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: []

5 Answers 5


You would need to create a test user which has a role defined, and then runas test user. With this all the account or contacts which are getting created will have Owner as this test user, and now when in your code the community user from your code it wont throw an error.

This test user is different from Community user, "test user" is just used to remedy the issue which you are facing in your test class.


This error comes when your community user' Account owner doesn't have any role. So if you are creating community user in test class and then check who is the account owner which you are using to create user. And then check if account owner has any role. If they don't have any role then first you need to assign them a role.

It will solve your problem.

  • Well I have finally hard coded the ownerid of the Account, which the owner has the Account role and they tried to run the test. But no luck. Account TAcc1 = new Account(Name = AccName, Org_tier__c = OrgTier, Account_Role__c = AccRole, Platform_Country__c = 'China', BillingCountry = 'China', Industry = 'Other', ParentId = ParentId, OwnerId = '00540000002GUaS'); Commented Dec 11, 2015 at 11:13

When a Contact is converted to Community User, the role of the Community User is determined based on the Role of the Owner who owns the Account of the Contact.

So when test is being executed, the Account will be inserted as the user who executes the test until otherwise used system.runAs(specificUser).

So to avoid the above error, make sure the person who executes the test(manually or by deploying) is assigned a role.

Hope it helps.


You can do something like this,

UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1]; User usr = [Select Id,UserRoleId from User where UserRoleId=:portalRole.Id]; System.RunAs(usr) { // All your test code here }

Another alternative is assign role to the user who is logged in while deployment/ running tests. And then run test in his context.

Thanks, Smriti


In my particular use case, I was creating a Person Account and a Contact separately, then attempting to link the two, and then use the ContactId to create a new Portal User. However, if you are creating a Person Account and a Portal User, it is simply sufficient to associate the PersonContactId from the Person Account with the ContactId field on the User, like so:

Account objAccount = [SELECT PersonContactId FROM Account WHERE ... LIMIT 1];

User newUser = new User();
newUser.ContactId = objAccount.PersonContactId;
insert newUser;

Credit: https://salesforce.stackexchange.com/a/46313/51566

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .