public class DeactivateUser {
public static void deactivate(String username) {
User user = [SELECT Id, IsActive FROM User WHERE Username = :username];
user.IsActive = false;
update user;
}
}
Run the below code in anonymous
DeactivateUser.deactivate('user@example.com');
This will deactivate the user with the username "user@example.com". Make sure you have the appropriate permissions to modify user records in Salesforce.
If this information helps, please mark the answer as best. Thank you
String usernameToDeactivate = 'exampleuser@domain.com';
List<User> usersToUpdate = [SELECT Id, IsActive FROM User WHERE Username = :usernameToDeactivate];
if (usersToUpdate.size() == 1) {
User userToUpdate = usersToUpdate[0];
userToUpdate.IsActive = false;
update userToUpdate;
System.debug('User ' + userToUpdate.Username + ' has been deactivated');
} else {
System.debug('User ' + usernameToDeactivate + ' not found or more than one user found with this username');
}
Try the below apex class
Run the below code in anonymous This will deactivate the user with the username "user@example.com".
Make sure you have the appropriate permissions to modify user records in Salesforce.
If this information helps, please mark the answer as best. Thank you