global interface SchedulerJob {
void setJobInfo(SchedulerJobInfo info);
List<SchedulerJobParamInfo> getParamInfos();
}
global interface SchedulerJobInfo {
String getJobId();
String getJobName();
String getRunExternalId();
String getRunId();
String getClassName();
String getParamsJson();
String getTimezoneId();
Datetime getStartedAt();
Map<String, Object> getParams();
Object getParam(String key);
String getString(String key);
Object getJson(String key, System.Type cls);
Map<String, Object> getJsonParam(String key);
Integer getInteger(String key);
Double getDouble(String key);
Date getDate(String key);
DateTime getDateTime(String key);
boolean getBoolean(String key);
void logDebug(String message);
void logError(String message, Exception e);
void saveLog();
}
global class SchedulerJobParamInfo implements Comparable {
global String name {get;set;}
global String label {get;set;}
global String dataType {get;set;}
global String description {get;set;}
global boolean required {get;set;}
global Object defaultValue {get;set;}
global Object value {get;set;}
public Integer compareTo(Object job2) {
return name.compareTo(((SchedulerJobParamInfo) job2).name);
}
}
Example
Scheduler provides many pre-built jobs. One such job is SchedulerMassDeleteJob
. That job makes use of Job Parameters to accept the query that needs to be run
to fetch the records to delete etc., That implementation is as follows.
global class SchedulerMassDeleteJob implements Database.Stateful, Database.Batchable<Sobject>, SchedulerJob {
private static List<SchedulerJobParamInfo> paramInfos = new List<SchedulerJobParamInfo>{
new SchedulerJobParamInfo('query', 'Query', SchedulerConstants.Type_LongText, true, 'The query which needs to be executed to identify the records to be deleted'),
new SchedulerJobParamInfo('hard_delete', 'Hard Delete?', SchedulerConstants.Type_Boolean, false, 'Should the records be hard deleted?')
};
public List<SchedulerJobParamInfo> getParamInfos() {
return paramInfos;
}
private SchedulerJobInfo jobInfo;
public void setJobInfo(SchedulerJobInfo jobInfo) {
this.jobInfo = jobInfo;
}
global Database.QueryLocator start(Database.BatchableContext btx){
String query = jobInfo.getString('query');
if (String.isBlank(query)) {
throw new SchedulerException('SchedulerMassDeleteJob: Job Parameter "query" cannot be blank');
}
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext btx, List<SObject> scope) {
Database.delete(scope, false);
if (jobInfo.getBoolean('hard_delete')) {
DataBase.emptyRecycleBin(scope);
}
}
global void finish(Database.BatchableContext btx) {
}
}