Schedule Batch Apex in Salesforce
Biswajeet
September 10, 2015 No Comments on Schedule Batch Apex in Salesforce
Batch Class:
global class accountBatch implements Database.Batchable<sobject> { global Database.QueryLocator start(Database.BatchableContext bc){ String query = 'SELECT Id, Name FROM Account'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext bc, List<account> scope) { for(Account a : scope) { a.Name = a.Name + 'Updated'; } update scope; } global void finish(Database.BatchableContext bc) { } }
Scheduled Class:
global class accountBatchSchedule implements Schedulable{ global void execute(SchedulableContext sc) { //invoke the batch class Database.executeBatch(new accountBatch()); } }
There are 2 ways to schedule an apex job:
- Scheduling a Job from the UI
- Using the System.Schedule Method
Go to Setup | Apex Class | Click Schedule Apex
- Enter Job name and select Apex Scheduled Schedulable.
- Select the Schedulable frequency Weekly or Monthly as per your requirement.
- Select the start and end dates.
- Select preferred start time.
- Click on Save.