+ Start a Discussion
CLitesCLites 

Apex test code for class that creates community users

Hi,

I am running in to an issue that I was hoping someone might have some insight in to. I have a method in a class triggered by contact insert or update. This method will check to see if a contact has a box checked and if it does, it will create a correspondinng user record with the ContactId set to the Id of the contact that was being created/edited. 

The issue that I am having is with the test code coverage. I am getting the error "UNKNOWN_EXCEPTION, portal account owner must have a role". Now I have seen the workaround for this by creating a user with a role and then putting the community user creation code inside System.runAs context with the user you created. This works for community users created in the test class itself, however, if the test class is not actually creating the community user directly, but rather by creating a contact which triggers the class mentioned above to create a community user, it fails with the same role error mentioned above. 

In all my searching, all I have found is a solution to the first issue where the community user is being created in the test class itself. I have not found any info for resolving it if the test class it then calling out to another class that creates the user. Any info on how I might be able to resolve would be greatly appreciated.

Thanks,

Chris
 
Prady01Prady01
Hi, I am a little confused. The community users are eabled from a button on contact. So Lets forget it. Let me see if I can answer your question.

You are an Admin and you are running the test class then make sure you have a role. Just make sure in which ever user (example usr99) context you are running the test class then that user should have a role defined. 

If ran the in the context of usr99 then usr99 should have a role defined in the user.

Hope this helps!
Prady01
Andrew GAndrew G
Prady is on the right path.

But also make sure the Owner of the test Account inserted has a Role also.  If you are doing a Test class and you have something like:
 
Insert Account;
Insert Contact;
Insert User;
System.runas(User);
test.start();
Invoke action to create community user;
test.stop();
SInce the account is inserted prior to the runas, it is inserted with owner ship of the person running the the test code - hence you.  Whilst the RunAs (User) then runs the remaining code as the "test user", the ownership of the created test account will still be against the System Admin.

You could resolve in one of a few ways.
1. With the one Runas User, ensure a Role is set, Invoke System.runas , then insert the Account and then invoke the code to test., 
2. Ensure that your System Admins always have a Role assigned, 
3. Create an "insert Account" user, and then create a "run code" user.  And then have multiple Runas statements in your code.

reference for multi Runas example: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

Regards
Andrew
CLitesCLites
Thanks folks. Turns out I am just an idiot. When trying to minimize my code to make it as simple as possible to post here, I ran it again and did not have an issue. It turns out that there was a contact where I did not assign an account and I just did not notice. I was so focused on the account having an owner and the owner having a role that I did not step back and realize that the contact did not have an account (and thus did not have an account owner with a role). Thanks for your replies though!