This question is more about with approach is ideal to follow.
Scenario: I need to udpate a field value in some of the records once every week. Should the logic to update the field be present directly in the Schedule class or should I have batch apex and then schedule that?
For example:
public class ScheduleApex implements schedulable {
public void execute(SchedulableContext sc){
// Query records here and update field
}
}
OR
public class ScheduleApex implements schedulable {
public void execute(SchedulableContext sc){
// create instance of BatchApex and execute that
}
}
public class BatchApex implements
Database.Batchable<sObject>{
public Database.QueryLocator start(Database.BatchableContext BC){
// Return query for fetching required records
}
public void execute(
Database.BatchableContext BC,
List<sObject> scope){
// update field for above records
}
}
public void finish(Database.BatchableContext BC){
}
}