I am working on the following scenario:

Batch job which runs everyday, collects all Closed Cases for that day(usually, every minute 200 Cases are closed), invoke rest api for each Closed Case on that day(api takes CaseId, UserId as parameters and gives key as response), and update the respective closed case with the key that is received from the api response. As there is 100 callout limit, I gave batch size as 100. Please find the code that I tried:

Batch Class:

global class CaseBatch implements Database.Batchable<sObject>, Database.AllowsCallouts
        global Database.QueryLocator start(Database.BatchableContext bc)
            //String soqlQuery = 'SELECT OwnerId,Secret_Key__c From Case Where (Status=\'Closed\' AND ClosedDate = TODAY)';
            String soqlQuery = 'SELECT OwnerId,Secret_Key__c From Case Where ClosedDate = TODAY';
            String soqlQuery = 'SELECT OwnerId,Secret_Key__c From Case Where Status=\'Working\' ';
            return Database.getQueryLocator(soqlQuery);
        global void execute(Database.BatchableContext bc, List<Case> cases)
            System.debug('====>> Size is '+cases.size());
            for(Case caseRecord : cases)
                    caseRecord.Secret_Key__c=RestCallout.makeCallout(caseRecord.Id, caseRecord.OwnerId);
                catch(Exception e)
                    System.debug('Error-' + e.getMessage());   
            UPDATE cases;
        global void finish(Database.BatchableContext bc)

Batch job execution:

CaseBatch be = new CaseBatch();
Database.executeBatch(be, 100); 

REST Callout:

public class RestCallout {
    public static String makeCallout(Id caseId, Id agentId)
        //Map<String, String> m = new Map<String, String>();
        String output;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String body = 'id='+caseId+'&agentid=test';
        HttpResponse response = http.send(request);    
        if (response.getStatusCode() == 200) 
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());          
            output = string.valueof(results.get('secretKey'));
    return output;        

I tried with some 300 closed cases and it is successfully updating the cases with the response. But if 200 cases get closed per minute, kindly let me know if there is any possibility of this code getting fail due to time outs or governor limits.

Thank you.

  • If you have any kind of control over the rest API I would suggest they upgrade it to handle bulk requests and return bulk results so you only need to make one callout. – Eric Sep 8 at 15:54

It depends on how long the API call itself takes. As long as you're not overwhelming the endpoint so it can handle the requests quickly, you should be fine. Note that at 100 calls per execution means the API must return a response within 1.2 seconds or you risk the 120 second callout limit. Your code only uses about 1.2% of the daily limits for asynchronous execution, so it should be just fine.

| improve this answer | |
  • I just came to know that "The API recently has had some performance issues and every now and again it times out." . In such case please let me know if reducing the batch size is the solution? – Suha Sep 8 at 14:36
  • When I tried the callout with 500 closed cases and batch size as 100, it is failing saying "api timed out". I tried with batch size 10 and still the api is getting timed out for some. Please let me know how to overcome this. – Suha Sep 9 at 4:18

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.