Salesforce Batch中調用接口

global class ErrorLogBatch implements Database.Batchable<sObject>,Database.AllowsCallouts
在implements後加上Database.AllowsCallouts
@Future(callout=true)方法去掉
例子:
global class RePushSMSTemplateBatch implements Database.Batchable<sObject>,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<InterfaceLog__c> scope) 
    {
        List<InterfaceLog__c> listlog = (list<InterfaceLog__c>)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<InterfaceLog__c> listQueue = Database.query(query);
        if(listQueue.size() > 0 && !System.Test.isRunningTest()){
            Database.executeBatch(new RePushSMSTemplateBatch(), 1);
        }
    }
}

發佈了54 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章