[Solved] INVALID_CROSS_REFERENCE_KEY, You can only assign users to permission set groups that have the “Updated” status

yassin0 Asks: INVALID_CROSS_REFERENCE_KEY, You can only assign users to permission set groups that have the “Updated” status
I’m struggling to assign a mocked permission set group in a test class. The test class fails because of the following error:

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

This is the apex test class:

Code:
@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.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.