[SalesForce] Deploying Permission Sets using Change Sets – came to destination org without the Assigned Users

In the source org, Permission Sets had various users assigned to them.
After deployment, Permission Sets in the destination org had no users in them.

All of the users are in both orgs with the same IDs.

How can I deploy user assignments along with the permission sets?

Best Answer

Permission Set Assignments are data in the org. The DML operation cannot be performed by a Change Set - but this sample code might help you in automating the assignments.

// the user reference
User u = new User();

// the names of the permission sets you need to assign
List<String> permissionSets = new List<String>{'permission_set_name_1','permission_set_name_2'};

// the assignment records for DML
List<PermissionSetAssignment> psAssignments = new List<PermissionSetAssignment>();

// query for the sets and assign to the user
for (PermissionSet ps : [SELECT Id, Name
                           FROM PermissionSet
                          WHERE Name IN :permissionSets]) {
    psAssignments.add(
        new PermissionSetAssignment(
            AssigneeId      = u.Id,
            PermissionSetId = ps.Id
        )
    );
}

// insert the assignments
insert psAssignments;
Related Topic