• Vinay Bhongale
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I'm trying to create a scheduled batch job that deactivates users that are older than 60 days but it seems to not be working. I believe that it's the query portion that is the culprit. 


global class DeactivateUsers implements Database.Batchable<sObject> {
     global Database.QueryLocator start(Database.BatchableContext bc) {
          return Database.getQueryLocator('SELECT Id, LastLoginDate FROM User WHERE IsActive = TRUE AND LastLoginDate > LAST_N_DAYS:60');
     }
     global void execute(Database.BatchableContext bc, List<User> records){
         List<User> userList = new list<User>{};

         for(User InactiveUser : records){
             InactiveUser.IsActive = false;
             userList.add(InactiveUser);
         }
         update userList;
     }

     global void finish(Database.BatchableContext bc){
         // execute any post-processing operations
     }
}