You need to sign in to do that
Don't have an account?
CLites
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
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
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
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:
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