0

I'm struggling to assign a mocked permission set group in a test class. The test class fails because of the following error:

INVALID_CROSS_REFERENCE_KEY, You can only assign users to permission set groups that have the "Updated" status.

This is the apex test class:

@isTest
private class UserPermissionControllerTest  {

   private static User createUserPermissionDataModel() {
       //permission set
       PermissionSet ps = new PermissionSet(
           Name = 'TestPermissionSet',
           Label = 'Test Permission Set'
       );
       insert ps;

       //permission set group
       PermissionSetGroup psg = new PermissionSetGroup(
           DeveloperName = 'TestPermissionSetGroup',
           MasterLabel = 'Test Permission Set Group'
           //Status = 'Updated'
       );
       insert psg;
       //permission set for group
       PermissionSet ps2 = new PermissionSet(
           Name = 'TestPermissionSetForGroup',
           Label = 'Test Permission Set For Group'
       );
       insert ps2;
       //permission set group component
       PermissionSetGroupComponent  psgc = new PermissionSetGroupComponent(
           PermissionSetGroupId = psg.Id,
           PermissionSetId = ps2.Id
       );
       insert psgc;

       //role
        UserRole role = new UserRole(
           Name = 'Developer',
           DeveloperName = 'Developer',
           CaseAccessForAccountOwner = 'Edit',
           OpportunityAccessForAccountOwner = 'Edit'
       );
       insert role;

       //user
       User permUser = new User(   
           FirstName = 'Test',
           LastName = 'User',
           Username = 'test.user@mock.de',
           Email = 'test.user@mock.de',
           Alias = 'testuser',
           TimeZoneSidKey = 'Europe/Berlin',
           LocaleSidKey =  'en_US',
           LanguageLocaleKey='en_US',
           EmailEncodingKey='UTF-8',
           profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id,
           UserRoleId = role.Id
       );
       insert permUser;
       //permission set assignment
       PermissionSetAssignment psa = new PermissionSetAssignment(
           AssigneeId = permUser.Id,
           PermissionSetId = ps.Id
       );
       insert psa;
       //permission set group assignment
       PermissionSetAssignment psga = new PermissionSetAssignment(
           AssigneeId = permUser.Id,
           PermissionSetGroupId = psg.Id
       );
       insert psga;
       
       return permUser;
   }
}

The status of the permission set group is null when I log the field. I don't know how to fix this issue and didn't find anything online that would point me to the right direction. Any help is highly appreciated.

UPDATE 1:

Added a test method that uses the method "createUserPermissionDataModel()" for additional input:

@isTest
private static void testGetUserPerm() {
    Test.startTest();
        User permUser = createUserPermissionDataModel();
        List<PermissionHelper.PermInfos> permInfos = UserPermissionController.getUserPerm(permUser.Id);
    Test.stopTest();
    System.assertNotEquals(null, permInfos);
}
10
  • Try using a Test.startTest/Test.stopTest pair around the permission set creation, with the assignment outside that block.
    – Phil W
    Aug 1 at 19:54
  • @PhilW I already use the test pair in my other methods that make use of the initial method. I added a method for reference.
    – yassin0
    Aug 1 at 20:09
  • Is the perm set assignment itself in the start/stop block, or after it?
    – Phil W
    Aug 2 at 6:13
  • @PhilW I call the "createUserPermissionDataModel()" method inside the test block so it's inside it
    – yassin0
    Aug 2 at 6:27
  • 1
    @cropredy a co-worker looked over my problem and fixed it for me. The problem for me was that the PSG status changed to 'Outdated' after inserting the PSG-Component and second PS. I'm gonna post the working code as an answer for reference.
    – yassin0
    Aug 3 at 11:44
1

The problem for me was that the status of the permission set group (PSG) changed after adding the PSG-Component and second Permission Set. I had to assign the PSG first before inserting new components. This fixed my problem:

//create user with permissions
public static User createUserPermissionDataModel() {
    //permission set
    PermissionSet ps = new PermissionSet(
         Name = 'TestPermissionSet',
         Label = 'Test Permission Set'
    );
    insert ps;
    //permission set group
    PermissionSetGroup psg = new PermissionSetGroup(
        DeveloperName = 'TestPermissionSetGroup',
        MasterLabel = 'Test Permission Set Group'
    );
    insert psg;
    //role
    UserRole role = new UserRole(
         Name = 'Developer',
         DeveloperName = 'Developer',
         CaseAccessForAccountOwner = 'Edit',
         OpportunityAccessForAccountOwner = 'Edit'
    );
    insert role;
    //user
    User permUser = new User(
         FirstName = 'test',
         LastName = 'user',
         Username = 'test.user@mock.de',
         Email = 'test.user@mock.de',
         Alias = 'testuser',
         TimeZoneSidKey = 'Europe/Berlin',
         LocaleSidKey = 'en_US',
         LanguageLocaleKey = 'en_US',
         EmailEncodingKey = 'UTF-8',
         profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id,
         UserRoleId = role.Id
    );
    insert permUser;
    //permission set group assignment
    PermissionSetAssignment psga = new PermissionSetAssignment(
         AssigneeId = permUser.Id,
         PermissionSetGroupId = psg.Id
    );
    insert psga;
    
    //permission set for group
    PermissionSet ps2 = new PermissionSet(
        Name = 'TestPermissionSetForGroup',
        Label = 'Test Permission Set For Group'
    );
    insert ps2;
    //permission set group component
    PermissionSetGroupComponent psgc = new PermissionSetGroupComponent(
         PermissionSetGroupId = psg.Id,
         PermissionSetId = ps2.Id
    );
    insert psgc;
    //permission set assignment
    PermissionSetAssignment psa = new PermissionSetAssignment(
        AssigneeId = permUser.Id,
        PermissionSetId = ps.Id
    );
    insert psa;

    return permUser;
}
@isTest
private static void testGetUserPerm() {
    Test.startTest();
    List<PermissionHelper.PermInfos> permInfos = UserPermissionController.getUserPerm(testUser.Id);
    Test.stopTest();
    System.assertNotEquals(null, permInfos);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.