WHAT IS THIS ERROR
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:
- While creating a user from apex like Registration handler or some other controller that creates External User.
- In test class while creating user.
RESOLUTION
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