1

我是 Apex 的新手,我必须为每个帐户(数千个帐户)调用一个网络服务。

通常,单个 Web 服务请求需要 500 到 5000 毫秒。

据我所知,此任务需要可调度和可批处理的类。

我的想法是按国家代码(仅限欧洲)对帐户进行分组,并为每个组开始一个批次。

第一批由 schedulable 类开始,下一批以批处理完成方法开始:

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

    private List<String> countryCodes;
    private countryIndex;

    global AccValidator(List<String> countryCodes, Integer countryIndex) {
        this.countryCodes = countryCodes;
        this.countryIndex = countryIndex;
        ...
    }

    // Get Accounts for current country code
    global Database.QueryLocator start(Database.BatchableContext bc) {...}

    global void execute(Database.BatchableContext bc, list<Account> myAccounts) {
        for (Integer i = 0; i < this.AccAccounts.size(); i++) {
            // Callout for every Account
            HttpRequest request ...
            Http http = new Http();
            HttpResponse response = http.send(request); 
            ...
        }
    }

    global void finish(Database.BatchableContext BC) {
        if (this.countryIndex < this.countryCodes.size() - 1) {
            // start next batch 
            Database.executeBatch(new AccValidator(this.countryCodes, this.countryIndex + 1), 200);
        }   
    }

    global static List<String> getCountryCodes() {...}
}

还有我的日程安排课:

global class AccValidatorSchedule implements Schedulable {
    global void execute(SchedulableContext sc) {
        List<String> countryCodes = AccValidator.getCountryCodes();
        Id AccAddressID = Database.executeBatch(new AccValidator(countryCodes, 0), 200);
    } 
}

现在我被 Salesforces 执行调控器和限制所困扰:对于几乎所有的标注,我都会收到“读取超时”或“超过为标注分配的最大时间(120000 毫秒)”的异常。

我也尝试过异步标注,但它们不适用于批处理。

那么,有没有办法安排大量的标注?

4

1 回答 1

0

您是否尝试将执行方法限制为 100?Salesforce 仅允许每笔交易 100 个标注。IE

Id AccAddressID = Database.executeBatch(new AccValidator(countryCodes, 0), 100);

也许这可能会对您有所帮助: https ://salesforce.stackexchange.com/questions/131448/fatal-errorsystem-limitexception-too-many-callouts-101

于 2016-07-18T19:42:01.863 回答