+ Start a Discussion
Diwakar G 7Diwakar G 7 

System.LimitException: Too many callouts: 101 Error

Hi,
I am trying to call api using executeAPI method. The response is jobid. Now, I am checking whether the job is completed or not using checkStatus method continuously in while loop. It will take around 5 min to complete the job.
 
Public class PageController{

        public static String getMethod(String instance_url,String token){
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(instance_url);
        req.setMethod('GET');
        req.setHeader('Authorization', 'Bearer '+token);

        HTTPResponse res = http.send(req);
        String json1 = res.getBody();
        return json1;        
    }

    public static void executeAPI(String token){ 
    String instance_url = 'https://api'; 
    String json1 = getMethod(instance_url, token); 
    return json1;
 }

      public static void checkStatus(String jobid,String token){
        String instance_url = 'https://api/job/status/'+jobid;
        String json1 = getMethod(instance_url, token);
        Map<String, Object> data1 = (Map<String, Object>) JSON.deserializeUntyped(json1);
        while(data1.get('isFinished') == False)
        {
           json1 = getMethod(instance_url, token);
           data1 = (Map<String, Object>) JSON.deserializeUntyped(json1);
        }
        System.debug('Success');
    }

    public static void mainFunction(){
      token = 'avdndbdn'
      String json1 = executeAPI(token)
      Map<String, Object> data1 = (Map<String, Object>) JSON.deserializeUntyped(json1);

     checkStatus(data1.get('JobId'),token)
     }

}

But, I am getting "System.LimitException: Too many callouts: 101 in API Call" error. Please help me.

Thanks and Regards,
Diwakar G
Dileep Singhal 18Dileep Singhal 18
Hi Diwakar,
It is salesforce governer limit that only 100 api callout is allowed in a single transaction. If this callout count breach the limit then salseforce apex engine throw exception. 
These docs should give you the limit info you need:
 
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_timeouts.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

In Posted code snippet , You are making api callout in a loop and not have any condition to break this loop after certain api callouts. So it should be modified according to salesforce limit.

Hope it will help .


Regards
Dileep