1

I'am new to Apex and I have to call a webservice for every account (for some thousands 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?

Samwise_71
  • 11
  • 4

1 Answers1

0

Have you tried to limit your execute method to 100? Salesforce only allows 100 callout per transaction. I.e.

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

Perhaps this might help you: https://salesforce.stackexchange.com/questions/131448/fatal-errorsystem-limitexception-too-many-callouts-101

Community
  • 1
  • 1