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

Salesforce DevTools – A powerful Chrome exte... Salesforce DevTools is a Chrome extension helping Force.com developers do the below things: ・Exporting Objects API Reference to Excel file. ・Generating Salesforce data model (ERDs) as svg. ・Show / Hide object API name on object detail page by 1 Click. ・Quickly generate test data Apex Code and ...
Pipeline for Salesforce – Be less busy, less... Overview Pipeline is a metadata deploy app for Salesforce, supports: ・Making Changeset with Excel file in seconds. ・Exporting Pipeline target metadata list to Excel file. ・No dependencies on Salesforce Org, deploying metadata between any different organizations. ・Deploying metadata base o...
Salesforce Mass Editor (Chrome Extension) – ... This's my second Chrome Extension (First one is HERE) this year, named "Salesforce Mass Editor" and it supports for doing the below things : ・Makes any Salesforce list view to be a powerful mass editor. ・Mass insert, mass clone, mass update, mass delete, ALL-IN-ONE. Support for both Classic and ...
Salesforce Smart Importer – 1-Drag to import... This is my third Chrome Extension named Salesforce Smart Importer (Other two popular extensions are Salesforce Mass Editor and Salesforce DevTools), it is designed for Salesforce not only admin but also normal user, supports : ・1-Drag to import any Excel / CSV data on anywhere in Salesforce, both ...