Automate with Ease: Scheduling Apex Jobs in Salesforce

Automate with Ease: Scheduling Apex Jobs in Salesforce 

When working with large datasets or periodic processes in Salesforce, manual execution of batch jobs can be time-consuming and error-prone. That’s where Scheduled Apex comes in — a powerful tool to automate and streamline recurring business operations. 

In this blog, we’ll explore what Scheduled Apex is, how to implement it, and walk through a real-world use case of automating data cleanup. 

🔄 What is Scheduled Apex? 

Scheduled Apex allows developers to schedule an Apex class to run at a specific time or interval — such as daily, weekly, or monthly — without manual intervention. It’s the ideal solution for periodic tasks like report generation, data archival, or batch updates. 

🛠 How to Create a Scheduled Apex Class 

Step 1: Implement the Schedulable Interface 

To make a class schedulable, it must implement the System.Schedulable interface. 

global class MyScheduler implements Schedulable { 

    global void execute(SchedulableContext SC) { 

        // Business logic here 

    } 

} 

 

Step 2: Embed Batch Job Logic 

If your scheduler is designed to invoke a batch process, initialize and execute the batch class within the execute() method. 

global class MyScheduler implements Schedulable { 

    global void execute(SchedulableContext SC) { 

        MyBatchClass batch = new MyBatchClass(); 

        Database.executeBatch(batch, 200); 

    } 

} 

🗓 How to Schedule the Job in Salesforce UI 

You can schedule your Apex class using the Salesforce setup UI: 

  1. Go to SetupApex Classes. 
  1. Click “Schedule Apex”. 
  1. Enter a Job Name. 
  1. Select your Apex class from the dropdown. 
  1. Choose the frequency: daily, weekly, or monthly. 
  1. Set the start time (e.g., 12:00 AM). 
  1. Click Save. 

This job will now run at your chosen interval automatically! 

📍 Tracking Scheduled Jobs 

To monitor scheduled jobs: 

  • Navigate to SetupMonitorJobsScheduled Jobs. 
  • Here, you’ll see details like: 
  • Status 
  • Next run time 
  • Frequency 
  • Associated class 

🔄 Real-World Use Case 

💼 Objective: 

Delete all Account records where the custom field Archive_Date__c is less than or equal to yesterday. Schedule the job to run daily at 12 AM. 

 

🧹 Batch Class — Delete Archived Accounts 

global class DeleteArchivedAccountsBatch implements Database.Batchable<SObject> { 

    global Database.QueryLocator start(Database.BatchableContext BC) { 

        Date yesterday = Date.today().addDays(-1); 

        return Database.getQueryLocator([ 

            SELECT Id FROM Account WHERE Archive_Date__c <= :yesterday 

        ]); 

    } 

 

    global void execute(Database.BatchableContext BC, List<Account> recordsToDelete) { 

        delete recordsToDelete; 

    } 

 

    global void finish(Database.BatchableContext BC) { 

        System.debug(‘Batch delete of archived accounts completed.’); 

    } 

} 

 

🕛 Scheduler Class — Runs Daily at Midnight 

global class ScheduleArchivedAccountDeletion implements Schedulable { 

    global void execute(SchedulableContext sc) { 

        DeleteArchivedAccountsBatch batch = new DeleteArchivedAccountsBatch(); 

        Database.executeBatch(batch, 200); 

    } 

} 

 

🧠 Pro Tips: 

  • Use descriptive names for your scheduled jobs to simplify tracking. 
  • Avoid overlapping schedules that could cause contention on records. 
  • Combine Scheduled Apex with email alerts or logging to monitor success or failure. 

Wrapping Up 

Scheduled Apex empowers you to automate repetitive tasks, reduce manual work, and improve system reliability. Whether you’re cleaning up data, sending out periodic notifications, or calculating metrics, scheduled jobs make Salesforce smarter — and your life easier. 

 

Leave a Comment

Your email address will not be published. Required fields are marked *