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


This error in apex code takes place when you are trying to create an external/community user using code and Account of this external user is assigned to such a User whose Role is blank. It may take places in below scenarios:

  1. While creating a user from apex like Registration handler or some other controller that creates External User.
  2. In test class while creating user.


Make sure that Account or Contact which is being assigned to User has a Role assigned.

For example:

User u = new User(); 
u.ContactId = [SELECT Id FROM Contact WHERE Account.Owner.UserRoleId != null];//Just for example