Created
March 2, 2022 17:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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