function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
guppster01guppster01 

Invalid Role Assignment in Test Class

I have a test class that works in my Developement Sandbox but doesn't work when Validating the Change Set in Production.

We have a Partner Community, and the test class creates a user with a Partner Community Role and Partner Profile.  Here is the User Creation Test code that is failing on Insert:
public static User getCommunityUser(Profile p, String contactId)
    {
        User testUser = new User();
        String rId = [SELECT ID FROM USERROLE WHERE NAME Like 'Partner Test - Big Dealer Partner User%' LIMIT 1].Id;
        

        testUser.Email = 'test@test.com';
        testUser.Username = '2345666@testuser.com';
        testUser.LastName = 'test';
        testUser.Alias = 'test';
        if (p != null)
         {
            testUSer.ProfileId = p.Id;
         }
         else
         {
            testUser.ProfileId = UserInfo.getProfileId();
         }
        testUser.LanguageLocaleKey = 'en_US';
        testUser.LocaleSidKey = 'en_US';
        testUser.TimeZoneSidKey = 'America/Chicago';
        testUser.EmailEncodingKey = 'UTF-8';
        testUser.ContactId = contactId;
        testUser.UserRoleId = rId;
        testUser.IsActive = true;

         insert testUser;  
         
         return testUser;
    }
This works in the Dev environments. 
The error when Validating in Production is this: 
User-added image
I have confirmed the Roles and Profiles exist in Production. Also confirmed I do have available licenses for the Community. I'm guessing there is a nuance with Partner Community Roles that I don't know about. 

Ideas? Answers? :) Thanks Community!
 
Best Answer chosen by guppster01
guppster01guppster01
Looked like the answer in this case was not to assign a role to the getCommunityUser created.
I commented out that line (line 24) and it worked

All Answers

guppster01guppster01
Looked like the answer in this case was not to assign a role to the getCommunityUser created.
I commented out that line (line 24) and it worked
This was selected as the best answer
AlvaroMattosAlvaroMattos
Great @guppster01 !! Thank you very much. I was facing same error creating a big list of partner users via Data Loader and your tip was amazing.