Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jdschleicher/bb9bab8c981fec6e04a0a2ffc91cfc3e to your computer and use it in GitHub Desktop.
Save jdschleicher/bb9bab8c981fec6e04a0a2ffc91cfc3e to your computer and use it in GitHub Desktop.
In the event of a refactor or another scenario where we want to remove permission set assignments from a list of specific users or all users in an environment
List<String> permissionSetAPINamesToRemove = new List<String>{
'permset_api_name_one',
'other_permset_api_name'
};
List<String> userIdsToRemoveAssignment = new List<String>{
'18characteruserid',
'18characteruseridTwo'
};
Boolean filterByUserIds = true;
List<PermissionSet> permissionSetsToRemove = [
SELECT Label,
PermissionsTransferAnyLead,
Id,
(SELECT AssigneeId,Assignee.Name FROM Assignments)
FROM PermissionSet WHERE Name in : permissionSetAPINamesToRemove
];
List<PermissionSetAssignment> permissionSetAssignmentsToRemove = new List<PermissionSetAssignment>();
for (PermissionSet ncaPermSetsToRemove : permissionSetsToRemove) {
for (PermissionSetAssignment assignment : ncaPermSetsToRemove.Assignments) {
if (!filterByUserIds) {
permissionSetAssignmentsToRemove.add(assignment);
} else if (userIdsToRemoveAssignment.contains(assignment.AssigneeId)) {
permissionSetAssignmentsToRemove.add(assignment);
}
}
}
System.debug('HERE IS NUMBER OF ASSIGNMENTS TO REMOVE : ' + permissionSetAssignmentsToRemove.size());
// delete permissionSetAssignmentsToRemove;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment