Users Freeze or UnFreeze in Salesforce using Script
User Freeze/Unfreeze information captured in the UserLogin Object.
User Freeze/Unfreeze is possible in two ways:
- Using DataLoader
- Execute scripts in Developer console -> Execute Anonymous Window
1. DataLoader
- Run an Export using DataLoader from the UserLogin object (Note: tick the “Show all Salesforce objects” checkbox)
- Select the Id, UserId, IsFrozen fields
- Edit the csv file and change the IsFrozen field to TRUE for any users you want to Freeze or FALSE to unfreeze.
- Run an Update using DataLoader on the UserLogin object and use the updated csv file you created above.
For More Information: https://help.salesforce.com/articleView?id=000187696&language=en_US&type=1
2. Scripts
#########################User-Freeze##############################
//Internal users – Freeze
List<UserLogin> listInternalUserFreeze = [select id, isfrozen, userid from UserLogin where userid in (select id from user where profile.name in (‘System Administrator’, ‘Sales Rep’) and isactive=true)];
if(listInternalUserFreeze != null && !listInternalUserFreeze.isEmpty()) {
for(UserLogin u : listInternalUserFreeze) {
u.isfrozen = true;
}
update listInternalUserFreeze;
}
//partner users – Freeze
List<UserLogin> listPartnerUserFreeze = [select id, isfrozen, userid from UserLogin where userid in (select id from user where profile.name in (‘Partner’) and isactive=true)];
if(listPartnerUserFreeze != null && !listPartnerUserFreeze.isEmpty()) {
for(UserLogin u : listPartnerUserFreeze) {
u.isfrozen = true;
}
update listPartnerUserFreeze;
}
#######################################################User-Unfreeze###########################
//Internal users – Unfreeze
List<UserLogin> listInternalUserFreeze = [select id, isfrozen, userid from UserLogin where userid in (select id from user where profile.name in (‘System Administrator’, ‘Sales Rep’) and isactive=true)];
if(listInternalUserFreeze != null && !listInternalUserFreeze.isEmpty()) {
for(UserLogin u : listInternalUserFreeze) {
u.isfrozen = false;
}
update listInternalUserFreeze;
}
//partner users – Unfreeze
List<UserLogin> listPartnerUserFreeze = [select id, isfrozen, userid from UserLogin where userid in (select id from user where profile.name in (‘Partner’) and isactive=true)];
if(listPartnerUserFreeze != null && !listPartnerUserFreeze.isEmpty()) {
for(UserLogin u : listPartnerUserFreeze) {
u.isfrozen = false;
}
update listPartnerUserFreeze;
}
To check user freeze/unfreeze in the user detail page of salesforce: