批处理作业为sObject运行两次

时间:2016-02-19 13:12:56

标签: salesforce batch-processing apex

我遇到的问题是我的批处理作业在记录上运行了两次,有人可以告诉我如何检查...

global class SVMXC_BatchCallClosureGlobal implements Database.Batchable<sObject>, Database.AllowsCallouts {

    public String query = 'SELECT Id, Case__c, Case__r.Sales_Organisation_From_IP__c, Re_Update__c, SVMXC_Batch_Picked_Up__c, Result__c,SVMX_Retry_XML__c, CreatedDate ' +
                          'FROM OutBoundMWLog__c ' +
                          'WHERE ((SVMXC_Batch_Picked_Up__c = false AND Result__c = null) ' +
                          'OR Re_Update__c = false OR SVMX_Retry_XML__c =true) and Case__r.Sales_Organisation_From_IP__c IN :globalSap';

    Set<String>globalSap = new Set<String>();
    List<Sales_Org_with_Region_and_Country__c> settingValues = Sales_Org_with_Region_and_Country__c.getall().values();

    public SVMXC_BatchCallClosureGlobal() {
        for (Sales_Org_with_Region_and_Country__c setting : settingValues) {
            if (setting.SAP_Application__c != null && setting.SAP_Application__c.equals('Global SAP')) {
                globalSap.add(setting.Sales_Org__c);
            }
        }
    }

    global Database.Querylocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List <sObject> scope) {
        Set<Id> caseIdSetforGlobal = new Set<Id>();
        Set<Id> caseIdSet = new Set<Id>();

        DateTime now = system.now().addMinutes(-3);

        for (OutBoundMWLog__c obound : (List<OutBoundMWLog__c>) scope) {
            if (obound.CreatedDate <= now || Test.isRunningTest()) {
                obound.SVMXC_Batch_Picked_Up__c = true;
                obound.Re_Update__c = true;
                caseIdSet.add(obound.Case__c);
            }
        }

        //Query for the case's Sales organisation from the caseIdSet.
        List<Case> caseList = new List<Case>([select id, Sales_Organisation_From_IP__c from case where id in: caseIdSet]);
        for (Case cs : caseList) {
            if (globalSap.contains(cs.Sales_Organisation_From_IP__c)) {
                caseIdSetforGlobal.add(cs.id);
            }
        }

        system.debug('******caseIdSetforGlobal*********' + caseIdSetforGlobal);

        if (!caseIdSetforGlobal.isEmpty()) {
            ServiceMaxPBIntegrationGlobal.ProcessFile(caseIdSetforGlobal);
        }

        update scope;
    }
}

1 个答案:

答案 0 :(得分:0)

如果批处理作业运行了不止一次,则意味着它已从其他地方调用。

强调某些可能性:

  • 可能是触发它的触发器,工作流或流程构建者。
  • 它可能已安排为定期运行。
  • 它可能是从电子邮件服务侦听器中调用的,而该侦听器可能是从您的代码中调用的。