Friday, August 7, 2015

Salesforce UNKNOWN_EXCEPTION, portal account owner must have a role: []

After being dropped into an existing Salesforce Org to make changes the first thing I like to do is run the existing test cases. It gives me a rough lay of the land and a feel for the health of Org.

Is it just scrapping by the 75% threshold? Or, worse still, are current test cases already failing in production?
These are things that are going to make change set deployments from a sandbox difficult. More so when combined with any further changes I want to make.

One such recent Org started with a large number of test cases failing with the message:

UNKNOWN_EXCEPTION, portal account owner must have a role: []

This was occurring when the test methods were attempting to insert a User with the "Customer Community Login User" profile. Oddly this wasn't occurring in the sandbox.

The test cases in question were inserting an Account, Contact, and User record that were all related to each other. From the error message, the Salesforce User that inserts that Account needs to have a role assigned. For some reason the Sandbox user I was provided had the Role set, and the production user didn't.

Setting my current users Role in production fixed that particular issue.

For good measure, I updated the test cases with the following to make the resolution more apparent.

// UNKNOWN_EXCEPTION, portal account owner must have a role: []
System.assertNotEquals(null, UserInfo.getUserRoleId(), 'The current user with Id: ' + UserInfo.getUserId() + 
             ' Name: ' + UserInfo.getUserName() + ' needs a Role assigned to test Portal/Community users');

Onward to fix the the remaining existing failures...