We want to run a Batch job that runs hourly and updates the users to Inactive if they are logged in last 2 Hours but not 30 Minutes. The Code works fine when I run it manually and but i need to run this job every hour forever until we stop. I did some research and it looks like we need to write a cron expression in Dev console however this doesn't seems to be working, there are no users who got updated and when i see apex jobs, there are no batches processed. Wonder If I'm doing it wrong.
//Batch class
global class ScheduleInactiveUser Implements Schedulable, Database.Batchable<sObject> {
@TestVisible
private DateTime LoggedInPast2Hours = System.now().addMinutes(-120);
@TestVisible
private DateTime LoggedInPast30Mins = System.now().addMinutes(-30);
global void execute(SchedulableContext sc) {
Database.executeBatch(this, 200);
}
global Database.QueryLocator start(Database.BatchableContext BC) {
string query = 'Select Id, LastLoginDate, IsActive, Name From User Where Profile_Name__c = \'Portal JIT User\' AND LastLoginDate >= :LoggedInPast2Hours AND LastLoginDate <=: LoggedInPast30Mins AND IsActive = TRUE';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
List<User> UserList = (List<User>) scope;
for(User c : UserList){
c.IsActive = False;
}
update UserList;
}
global void finish(Database.BatchableContext BC) {
}
}
I'm running below code in Dev Console
System.schedule('Hourly Job to InActivate Portal Users', '0 0 * * * ?', new ScheduleInactiveUser());
I can see the scheduled job for next hour as well, but it just doesn't seems to be working. I wonder if the above CRON expression is correct?. Am I doing something wrong?