2

I'am new to Apex and I have to call a webservice for every account (for a large number of accounts).

Usualy a single webservice request takes 500 to 5000 ms.

As far as I know schedulable and batchable classes are required for this task.

My idea was to group the accounts by country codes (Europe only) and start a batch for every group.

First batch is started by the schedulable class, next ones start in batch finish method:

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() {...}
}

And my schedule class:

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

Now I'am stuck with Salesforces execution governors and limits: For nearly all callouts I get the exceptions "Read timed out" or "Exceeded maximum time allotted for callout (120000 ms)". I also tried asynchronous callouts, but they don't work with batches.

So, is there any way to schedule a large number of callouts?

1

Since there's a 500 to 5,000ms delay, you might need to reduce your batch size. Instead of using 200, consider using a smaller value like 10, which should fit comfortably within the operating specifications of your service. Also, Read Timed Out means you're using the default timeout of 10,000, so you might also change HttpRequest's timeout to a higher value, such as 20,000.

Finally, consider adding a try-catch block around your send request to catch a CalloutException; if you get one, you can retry it later in a different batch. You'll want to set up a way to record which records have already been integrated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.