Batch 调用 定时执行

本文介绍了如何设置定时任务来执行批量操作,特别是在批处理过程结束后,如何触发另一个batch任务的执行,实现流程自动化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定时执行 batch 

global class LogistiInfoSchedu implements Schedulable, Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
    public Set<Id> updateOiIdSet;

    global void execute(SchedulableContext sc) {
        LogistiInfoSchedu lo = new LogistiInfoSchedu();
        database.executeBatch(lo,90);
    }

    //Start 方法里面查询的是系统中发货状态不为到达的发货申请的记录
    global Database.QueryLocator start(Database.BatchableContext bc) {
        updateOiIdSet = new Set<Id>();
        List<Order_Inquiry__c> qiList = new List<Order_Inquiry__c>();
        qiList = [select id,OrderStatus__c,OTNumber__c,SAP_order_number__c from Order_Inquiry__c where OrderStatus__c != 'delivered' and OrderStatus__c != 'epod' and OTNumber__c != NULL AND OTNumber__c != '' AND (ApprovalStatus__c = 'Approved' OR ApprovalStatus__c = 'Auto Approved')];
        System.debug('LogistiInfoSchedu 查到的数量:'+qiList.size());

        return Database.getQueryLocator([select id,OrderStatus__c,OTNumber__c,SAP_order_number__c from Order_Inquiry__c where OrderStatus__c != 'delivered' and OrderStatus__c != 'epod' and OTNumber__c != NULL AND OTNumber__c != '' AND (ApprovalStatus__c = 'Approved' OR ApprovalStatus__c = 'Auto Approved')]);
    }

    global void execute(Database.BatchableContext BC,list<Order_Inquiry__c> scope) {
        //调用接口解析xml 数据
        LogisticsInfoInterface lo = new LogisticsInfoInterface();
        Set<String> otmNymberSet = new Set<String>();

        for(Order_Inquiry__c ord : scope){
            otmNymberSet.add(ord.OTNumber__c);
        }

        system.debug('Batch批次中要处理:'+otmNymberSet);
        system.debug('Batch批次中要处理的数量:'+otmNymberSet.size());

        // httpResponse xmlRes = lo.getLogisticsInfo(otmNymberSet);
        // String responsStr = xmlRes.getBody().Replace('</time>','</theTime>');
        // responsStr = responsStr.Replace('<time>','<theTime>');

        SaveDeliveryInfo s = new SaveDeliveryInfo();
        s.getResponseXML(otmNymberSet);

        //updateOiIdSet.addAll(s.locationUpdateIdSet);
    }

    global void finish(Database.BatchableContext BC) {
        if(updateOiIdSet.size() > 0){
            UpdateLocationBatch thisBatch = new UpdateLocationBatch(updateOiIdSet);
            thisBatch.startUpdateLocation();
        }
    }
}

Finish 中调用其他batch

global class UpdateLocationBatch implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
    public Set<Id> locationUpdateIdSet;

    global UpdateLocationBatch() {
        locationUpdateIdSet = new Set<Id>();
    }

    global UpdateLocationBatch(Set<Id> orderIdSet) {
        locationUpdateIdSet = new Set<Id>();
        if(orderIdSet != NULL){
            locationUpdateIdSet = orderIdSet;
        }
    }

    /**
     * @Function [该Batch的启动方法]
     */
    public void startUpdateLocation(){
        UpdateLocationBatch thisBatch = new UpdateLocationBatch(locationUpdateIdSet);
        Database.executeBatch(thisBatch, 1);
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT Id, Latitude__c, Longitude__c, Location__c
                                        FROM Order_Inquiry__c
                                        WHERE Id IN: locationUpdateIdSet
                                            AND Latitude__c != NULL
                                            AND Longitude__c != NULL]);
    }

    global void execute(Database.BatchableContext BC, list<Order_Inquiry__c> scope) {
        Order_Inquiry__c thisOrder = scope.get(0);

        GetCurrentLocationService loc = new GetCurrentLocationService();
        thisOrder.Location__c = loc.getCurrentLocation(thisOrder.Latitude__c, thisOrder.Longitude__c);
        System.debug('thisOrder.Location__c='+thisOrder.Location__c);
        update thisOrder;
    }

    global void finish(Database.BatchableContext BC) {

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值