Skip to content

Instantly share code, notes, and snippets.

@SpenceDiNicolantonio
Last active January 26, 2021 21:20
Show Gist options
  • Save SpenceDiNicolantonio/ad02cdfe38643c96d92e2971a1b88f8f to your computer and use it in GitHub Desktop.
Save SpenceDiNicolantonio/ad02cdfe38643c96d92e2971a1b88f8f to your computer and use it in GitHub Desktop.
[Mass-Deactivate Salesforce Users] Deactivates all users that aren't assigned one of the profiles definied in a set #salesforce #apex
// Profiles to leave enabled
Set<String> excludedProfiles = new Set<String> {
'System Administrator',
'Integration System Administrator',
'AccessTSC Digital Profile',
'TriState Onboarding Team',
'Tristate Administrator'
};
List<User> usersToUpdate = new List<User>();
for (User u : [
select Id, Profile.Name, IsActive
from User
where Profile.Name != null
and Profile.Name not in :excludedProfiles
]) {
if (u.IsActive) {
u.IsActive = false;
usersToUpdate.add(u);
}
}
if (usersToUpdate.size() > 0) {
update usersToUpdate;
}
System.debug('Deactivated ' + usersToUpdate.size() + ' users');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment