Salesforce Batch中调用接口

global class ErrorLogBatch implements Database.Batchable,Database.AllowsCallouts
在implements后加上Database.AllowsCallouts
@Future(callout=true)方法去掉
例子:
global class RePushSMSTemplateBatch implements Database.Batchable,Database.Stateful,Database.AllowsCallouts
{
    public String query;

    public RePushSMSTemplateBatch()
    {
        query = 'SELECT Id,OwnerId,PhoneOrder__c,PhoneOrder__r.Consumer__c,PhoneOrder__r.Consumer__r.FirstPhone__c,SMSTemplate__c,SMSTemplate__r.Name,SMSTemplate__r.TemplateId__c,SMSTemplate__r.SMSText__c,SMSTemplate__r.ReplaceSendText__c,SMSTemplate__r.PushUserId__c,ReceiveStatus__c,RepetitionTimes__c,IsProcessed__c FROM InterfaceLog__c WHERE IsProcessed__c = false';
    }   

    global Database.QueryLocator start(Database.BatchableContext bc) 
    {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list scope) 
    {
        List listlog = (list)scope;
        for(InterfaceLog__c log : listlog)
        {
            //如果是发送短信错误
            if(log.PhoneOrder__c != null && log.SMSTemplate__c != null)
            {
                ReSendMessage.sendSMSTemplate(log.Id,log.PhoneOrder__c,log.PhoneOrder__r.Consumer__c,log.PhoneOrder__r.Consumer__r.FirstPhone__c,log.SMSTemplate__r.TemplateId__c,log.SMSTemplate__r.ReplaceSendText__c);
            }
            //如果是推送短信模板错误
            else if(log.SMSTemplate__c != null && log.PhoneOrder__c == null)
            {
                RePushSMSTemplateController.pushSMSTemplate(log.Id,log.SMSTemplate__r.PushUserId__c,log.SMSTemplate__c,log.SMSTemplate__r.Name,log.SMSTemplate__r.SMSText__c);
            }
        }
    }

    global void finish(Database.BatchableContext BC) 
    {
        //队列中存在尚未处理完的数据,重新启用batch。
        List listQueue = Database.query(query);
        if(listQueue.size() > 0 && !System.Test.isRunningTest()){
            Database.executeBatch(new RePushSMSTemplateBatch(), 1);
        }
    }
}

你可能感兴趣的:(Batch)