To fix “portal account owner must have a role” Error in Salesforce Community Unit Test

In Salesforce, if you’re trying to run Unit Test with a Non-Role user, which includes part of creating Portal / Community User test data, the following error will be occured, that is because the portal account’s owner does not have a role to be assigned.

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

Here is the sample code that can explain this case.

Solution

The fix this kind of error, you can assign a role in User edit page,
User Edit  xgeek   Salesforce   Developer Edition Or run as a user who has a UserRole in test code, as the below code.



Enjoy it!

Related Post

Setup an awesome Salesforce developer environment ... Why Sublime Text 3 ? I've been working with Force.com for about 2 years, and always using Eclipse+Force.com IDE(See here, Eclipse Force.com IDE Installation)as my development environment. I don't think it is really difficult to use, but it could be more friendly. For example: 1. It doesn't suppo...
Salesforce DevTools for Chrome™ Salesforce DevTools is a Chrome extension helping Force.com developers do the below things: ・Generate Salesforce data model (ERDs) as svg. ・Export definition of objects to Excel file. ・Show / Hide object API name on object detail page by 1 Click. ・Quick generate test data Apex Code and SOQL. ...
Using URL parameters on Salesforce standard pages Salesforce has many URL parameters can be used on its standard page, with this kind of parameters, you can do some URL hacking on it, as below: Note: All of parameters must be encoded with UTF-8. Login page There are two ways to login and access into Salesforce from url. 1. https://
Write Apex Test Classes efficiently with Tesforce 1.Why Tesforce The Test Classes are required to deploy Apex to a production environment or to be packaged and placed on Force.com AppExchange, it must provide at least 75% code coverage. That will be a huge workload to SFDC Developers if the project(application) include a lot of Apex Classes,the...