function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alfia Quadri 11Alfia Quadri 11 

Apex callout, need to iterate through pages

I am new to apex I am making an api call from salesforce to get contacts from another application. I am receiving contacts but in pages, I need to parse the response and make a call again to get the next page, What am I doing wrong that I get APEX CPU Execution time limit exceeded
The response I get in my first call is a list of contacts and
pages": {
        "type": "pages",
        "next": {
            "page": 42,
            "starting_after": "WzE2NjQ0MjUzM"
        },
        "page": 41,
        "per_page": 150,
        "total_pages": 56
    }

I need to send it this way in the request to query the next page
 
{
 "query":  {
    "field": "last_seen_at",
    "operator": ">",
    "value": "1646149510"
  },
  "pagination": {
    "per_page": 5,
    "starting_after": "1HaSB+xrOyyMXAkS"
  } 
}
Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://api.intercom.io/contacts/search/');
    req.setMethod('POST');
    String authorizationHeader = 'Bearer ' + 'dG9rOjM5N=';
    req.setHeader('Authorization', authorizationHeader);  
    req.setHeader('Accept', 'application/json');
    req.setHeader('Content-Type', 'application/json');
    req.setHeader('Intercom-Version', '2.6');
    Map<String, Object> m1 = new Map<String, Object> 
                { 
                    'value' => '1646149510',
                    'operator' => '>', 
                    'field' => 'last_seen_at'
                        };
                           

    
    String jsonBody = JSON.serialize(
    new Map<String, Object> {'query'=>m1}
    );
   
    req.setBody(jsonBody);
   
        HttpResponse res = h.send(req);
        Map<String, Object> results = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
        List<Object> listObject = (List<Object>)results.get('data');
        Map<String, Object>  pagesObject,NextpagesObject;
        pagesObject  = (Map<String, Object>)(results.get('pages'));
        NextpagesObject = (Map<String, Object>)(pagesObject.get('next'));
        while(NextpagesObject!=null)
        {
          Integer page = Integer.valueOf(NextpagesObject.get('page'));
          String starting_after=String.valueOf(NextpagesObject.get('starting_after'));
        
          Http h2 = new Http();
          HttpRequest req2 = new HttpRequest();
          req2.setEndpoint('https://api.intercom.io/contacts/search/');
          req2.setMethod('POST');
          String authorizationHeader2 = 'Bearer ' + 'dG9rOjM5N';
          req2.setHeader('Authorization', authorizationHeader2);  
          req2.setHeader('Accept', 'application/json');
          req2.setHeader('Content-Type', 'application/json');
          req2.setHeader('Intercom-Version', '2.6');
          
          Map<String, Object> m2 = new Map<String, Object> 
                { 
                    'value' => '1646149510',
                    'operator' => '>', 
                    'field' => 'last_seen_at'
                        };


                           
          Map<String, Object> m3 = new Map<String, Object> 
                {
                   'per_page'=> page,
                   'starting_after'=> starting_after
                } ;
                           
    
             String jsonBody2 = JSON.serialize(
                   new Map<String, Object> {'query'=>m2,
                              'pagination'=>m3}
                 );

   
         req2.setBody(jsonBody2);
         HttpResponse res2 = h.send(req2);
         Map<String, Object> results2 = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
         listObject = (List<Object>)(results2.get('data'));
         pagesObject  = (Map<String, Object>)(results2.get('pages'));
         NextpagesObject = (Map<String, Object>)(pagesObject.get('next'));
        
    }


 
SwethaSwetha (Salesforce Developers) 
HI Alfia,
1. This error generally occurs if transactions consume too much CPU time. Salesforce has a timeout limit for transactions based on CPU usage. If transactions consume too much CPU time, Salesforce shut them down as a long-running transaction.
 
2. To fix the issue, you need to optimize the code involved following the best practices described in the below articles.
 
>> https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1
>> https://developer.salesforce.com/page/Apex_Code_Best_Practices
 
3. Please use the following limit methods in your code to debug the amount of CPU time currently used in the transaction:
 
getCpuTime()
Returns the CPU time (in milliseconds) accumulated on the Salesforce servers in the current transaction.
 
getLimitCpuTime()
Returns the time limit (in milliseconds) of CPU usage in the current transaction.
 
Related:https://salesforce.stackexchange.com/questions/283103/apex-cpu-time-limit-error
 
https://salesforce.stackexchange.com/questions/319277/how-to-debug-which-process-is-slowing-my-updates explains how to debug this issue using developer console

If this information helps, please mark the answer as best. Thank you