Apex计划定期批处理作业

时间:2018-08-12 08:08:54

标签: salesforce apex schedule

我有Schedule类,用于计划Main类的下一次运行,而Batch类则用于更新User对象 我需要每天在特定时间运行批处理作业。 Main类执行Callout方法 我收到一个例外:“不支持从排定的Apex调用”。我也没有任何显示的System.debug日志

主班课程表:

global with sharing class MainSchedule implements Schedulable, Database.AllowsCallouts {

private String hebrewDate;

public MainSchedule() {

}

public String getHebrewDate() {
    return this.hebrewDate;
}

public void setHebrewDate(String hebrewDate) {
    this.hebrewDate = hebrewDate;
}

global void execute(SchedulableContext sc) {
    StartUpdateProcess sup = new StartUpdateProcess();
    sup.setBool(true);
    sup.mainProcess();
}

}

运行所有必需的方法和标注的主类:

public class StartUpdateProcess {

private Date theDate = Date.today();
private Integer theYear = theDate.year();
private Integer theMonth = theDate.month();
private Integer theDay = theDate.day();
private String apiSunset;
private String israelSunset;
private String hebrewDate;
private String timeForSchedule;
private Boolean bool = false;
private String jobId = '';

public StartUpdateProcess() {

}

public void mainProcess() {
    System.debug('Bool value: ' + this.bool);
    if (this.bool == true) {
        System.debug('Boolean for Tomorrow: ' + this.bool);
        scheduleForTomorrow();
    }
    getApiAndIsraelSunset();        //1
    getHebrewDate();                //2
    updateUserBatch();              //***
    getTimeForSchedule();           //3
    nextSchedule();                 //***
    //scheduleExecuteBatchJob();        //4
}

public void setBool(Boolean bool) {
    this.bool = bool;
}

private void getApiAndIsraelSunset() {
    try {
        SunsetCalloutServiceRest sc = new SunsetCalloutServiceRest(this.theYear, this.theMonth, this.theDay);
        sc.sendSunsetCallout();
        this.apiSunset = sc.getSunsetString();
        this.israelSunset = sc.getIsraelSunset();
        System.debug('API Sunset: ' + this.apiSunset);
        System.debug('Israel Sunset: ' + this.israelSunset);
    } catch (Exception e) {
        throw new HebrewDateSystemException('Sunset Callout Exception has occurred: ' + e.getMessage());
    }
}

private void getHebrewDate() {
    try {
        HebCalCalloutServiceRest hc = new HebCalCalloutServiceRest(this.theYear, this.theMonth, this.theDay);
        hc.sendHebCalCallout();
        this.hebrewDate = hc.getHebrewString();
        System.debug('Hebrew Date: ' + this.hebrewDate);
    } catch (Exception e) {
        throw new HebrewDateSystemException('HebCal Callout Exception has occurred: ' + e.getMessage());
    }
}

private void getTimeForSchedule() {
    this.timeForSchedule = '00 45 10 12 8 ? 2018';
    System.debug('Today\'s schedule: ' + this.timeForSchedule);
    if (this.bool == true) {
        ParseSunsetTime pst = new ParseSunsetTime(this.apiSunset);
        this.timeForSchedule = pst.parseSunsetForSchedule() + ' ' + this.theDay + ' ' + this.theMonth + ' ? ' + this.theYear;
        System.debug('Tomorrow\'s schedule: ' + this.timeForSchedule);
    }
}

private void scheduleForTomorrow() {
        System.debug('Today: ' + theDate);
        Date theTomorrow = Date.newInstance(this.theYear, this.theMonth, this.theDay);
        theTomorrow += 1;
        System.debug('Tomorrow: ' + theTomorrow);
        this.theYear = theTomorrow.year();
        this.theMonth = theTomorrow.month();
        this.theDay = theTomorrow.day();
        System.debug('Year: ' + theYear);
        System.debug('Month: ' + theMonth);
        System.debug('Day: ' + theDay);
}

private void updateUserBatch(){
    UpdateUserHebrewDateBatch updateUserBatch = new UpdateUserHebrewDateBatch(this.hebrewDate);
    database.executebatch(updateUserBatch);      
}

private void nextSchedule() {
    MainSchedule ms = new MainSchedule();
    ID jobID = System.schedule('Update Hebrew Date Job', this.timeForSchedule, ms);
}

}

Batch类:

global with sharing class UpdateUserHebrewDateBatch implements Database.Batchable<sObject>{
global final String Query;
global final String Value;
global final String Email;
global final Double latitude;
global final Double longitude;

global UpdateUserHebrewDateBatch() {
}

global UpdateUserHebrewDateBatch(String v) {
    this.Email = 'g***@***.com';
    this.Query = 'SELECT Hebrew_date__c, Latitude__c, Longitude__c FROM User WHERE IsActive = TRUE';
    this.latitude = 32.109333;
    this.longitude = 34.855499;
    this.Value = v;
}

global Database.QueryLocator start(Database.BatchableContext BC) {
    system.debug('Batch Start:');
    system.debug(BC.getJobId());
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope) {
    List<User> users = (List<User>) scope;
    for(User u : users) {
        u.Hebrew_date__c = Value;
        if(u.Latitude__c != this.latitude) {
            u.Latitude__c = this.latitude;
        }
        if(u.Longitude__c != this.longitude) {
            u.Longitude__c = this.longitude;
        }
    }
    update users;
}

global void finish(Database.BatchableContext BC){
    /*
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(new String[] {Email});
    mail.setReplyTo('batch@acme.com');
    mail.setSenderDisplayName('Batch Processing');
    mail.setSubject('Batch Process Completed');
    mail.setPlainTextBody('Batch Process has completed.');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    */

}

}

0 个答案:

没有答案