Last active
January 26, 2021 21:20
-
-
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
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
// 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