User Story
GIVEN I'm a System Admin,
WHEN a System Admin is inactive for 30 days,
THEN I want them to be automatically deactivated.
Solution
Apex Method:
global class DeactivateInactiveSystemAdmin implements Schedulable {
global void execute(SchedulableContext sc) {
deactivateInactiveSystemAdmin();
}
private void deactivateInactiveSystemAdmin() {
List<User> inactiveAdmins = [SELECT Id, LastLoginDate FROM User WHERE Profile.Name = 'System Administrator' AND IsActive = true AND LastLoginDate < LAST_N_DAYS 30];
for (User admin : inactiveAdmins) {
admin.IsActive = false;
}
update inactiveAdmins;
}
}
String jobName = 'DeactivateInactiveAdminJob';
String cronExp = '0 0 0 * * ?';
DeactivateInactiveSystemAdmin jobInstance = new DeactivateInactiveSystemAdmin();
System.schedule(jobName, cronExp, jobInstance);
Video
Video does not exists.