Skip to content

Instantly share code, notes, and snippets.

@Katulka
Last active December 1, 2016 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Katulka/016811a6eca7b700184a3e3a32d71587 to your computer and use it in GitHub Desktop.
Save Katulka/016811a6eca7b700184a3e3a32d71587 to your computer and use it in GitHub Desktop.
Using Execute Anonymous to freeze/unfreeze in bulk: We found this to be more straightforward for developers, and control feels more granular/confident. TIP: Do this in a sandbox before trying in your production environment. Script 1 will gather any existing frozen users so you don’t accidentally “Unfreeze” them in the last step. Script 2 does th…
// Script #2 (modify and repeat)
// script for FREEZING/UNFREEZING users in order to perform administrative tasks
// CONTINUE to run this script over and over depending on how many users are in the org (see batch size below)
// Review the DML rows portion of the debug log, run until that says 0
// TODO ----------------------------------------------------------------------------------------------------------------------------
// size of query for userslogin records and DML
Integer batchSize = 2000;
//TIP: Set batchSize to 0 and run this to review the debugs, verifying profile name etc.
// get sys-admin profiles for exclusion - ENTER ANY CUSTOM PROFILES FOR SYSTEM ADMINISTRATORS
list<Profile> admins = [SELECT id FROM Profile WHERE Name IN ('System Administrator', 'Premier Support User')];
// MODE: Set this to true or false depending on what mode you are running in
Boolean freezeUsers = true;
// IF IN UN-FREEZE MODE - TODO
set<id> usersToSkip = new Set<id>();
// FILL THIS IN from the system debug in the first script, when you unfreeze users and don’t forget to UNCOMMENT THIS LINE
//usersToSkip =new Set<id>{'', '', ''};
// --------------------------------------------------------------------------------------------------------------------------------------
System.debug('admin: ' + admins); // double check
// get profiles to be included
list<Profile> pro = [SELECT id, Name FROM Profile WHERE id Not IN :admins ORDER BY Name];
// add to set
set<id> proSet = new set<Id>();
for (Profile p : pro) {
system.debug('Non admin: ' + p.Name); // double check
proSet.add(p.Id);
}
// script will need to be modified to work for orgs with > 50k users - including active community users etc.
// query UserLogin with SubQuery condition for users with non-sysadmin profiles, limit to run faster and because >10k users
list<UserLogin> uls = [SELECT Id, UserId, isFrozen FROM UserLogin WHERE isFrozen !=: freezeUsers AND UserID IN (SELECT id FROM User WHERE isActive = true AND ProfileId IN :proSet AND Id NOT IN : usersToSkip) LIMIT : batchSize];
system.debug('userLogin: ' + uls.size()); // double check size against dml rows after run
// freeze / unfreeze users
for (UserLogin u : uls)
u.isFrozen = freezeUsers;
// update UserLogin object
if (!uls.isEmpty())
update uls;
else
system.debug('*** 0 records updated. Process complete.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment