Im trying to test if I can request an API without timeout or reach the sfdc api callout limit.. I want to loop the request because the API only show the response for the 1st 25 records of each page number.. there`s total 36 page, and I want to loop the api request while page number < page count.. I cant see the debug because the log only show blank page and if I download the log, its says that debug reach the limit.. is this code correct?

Integer pageNumber = 1;
    Integer pnumber = 0;
    Integer pcount = 1;

    while (pnumber < pcount) {

    Http h2 = new Http();
    HttpRequest req2 = new HttpRequest();
    req2.setHeader('content-type', 'application/json');
    req2.setHeader('Authorization', 'Bearer '+ token);

        HttpResponse res2 = new HttpResponse();
        res2 = h2.send(req2);
            JSONParser parser2;
            //if (test.isRunningTest()) parser2 = UserDetailResponseDummy;
            if (test.isRunningTest()==false) parser2 = JSON.createParser(res2.getBody());
            while (parser2.nextToken() != null){
            if(parser2.getCurrentToken() == JSONToken.FIELD_NAME) {
                if (parser2.getText() == 'pageNumber'){
                    pnumber = Integer.valueof(parser2.getText());
                    system.debug('pnumber ==>'+parser2.getText());
                else if (parser2.getText() == 'pageCount'){
                    pcount = Integer.valueof(parser2.getText());
                    system.debug('pcount ==>'+parser2.getText());


        catch(exception e){
    system.debug('Page Number ==>'+pageNumber);
    }//end while

You'll need to become quite familiar with the Callout Limits and Limitations documentation.

The limit on callouts in a transaction is 100, so 36 is theoretically okay provided that you are not running these operations in a transaction that's also making a lot of callouts to other systems.

The more important limit to note, since you're setting your timeout to 120,000 milliseconds, is the maximum cumulative timeout in a transaction:

The maximum cumulative timeout for callouts by a single Apex transaction is 120 seconds. This time is additive across all callouts invoked by the Apex transaction.

You'll have to determine whether this is a concern or not based on the performance of the API you are calling.

There are some other issues with your code.

        if (test.isRunningTest()==false) parser2 = JSON.createParser(res2.getBody());
        while (parser2.nextToken() != null){

This will throw a NullPointerException in test context, because you don't initialize parser2. You should be using a Mock to test your callouts.

    catch(exception e){

Catch blocks that do nothing but swallow the exception are very bad practice. Don't catch an exception you aren't going to take some action to recover from, and remember that you cannot catch limits exceptions at all.

The fact that your API returns the pnumber and pcount values you're using in your loop makes your code uniquely vulnerable to the remote API's behavior. If it changes these values, you could easily enter an endless loop or a loop long enough to throw limits exceptions. I would want to build some sanity checks for these values.

  • my debug reached the maximum debug log size, how can I make the debug size smaller? My log only showing the parsing process.. – Arief Gunawan Tjiptorahardja Nov 2 at 2:39

Code looks okay.

Please try System.debug(LoggingLevel.ERROR, 'message'); instead of System.debug('message'); and set the Debug Profile for Logs to show Error only.

That way you can only see the debug logs that you have mentioned and remove all the System info.

  • I just realized that my debug log reached the maximum debug log size.. I tried using debug loggingLevel but nothing happen.. – Arief Gunawan Tjiptorahardja Nov 2 at 2:34
  • Nvm thanks Akshay, I changed the logs level from the developer console and now its works fine.. the log showed my debug.. – Arief Gunawan Tjiptorahardja Nov 2 at 2:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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