[SalesForce] Test Class Issue with UNKNOWN_EXCEPTION, portal account owner must have a role

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: []

Best Answer

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.

Related Topic