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 difficult to use, but it could more friendly. For example: 1. It doesn't support APEX an...
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...
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. ・Export record data to CSV fi...
KISS principle for apex code with List and Map in ... Here are some KISS (Keep it simple, stupid) principles for cleaning our apex code with List and Map in Salesforce, as Linus Torvalds said: "Talk is cheap. Show me the code", let me show you as below: Initialize Map Normal https://gist.github.com/xgeek-net/cb98cf43ef51a72abf3fac01a141f381 KIS...