0

I am working with an API that returns 20 records per page and I need to make multiple API calls to get all the records based on the number of pages and total record count. The API returns these fields in json format that I hope to use for paging.

TotalRecords (example 213)
TotalNumberPages (11)
CurrentPage (1)

I am trying to modify my code so it will loop over the first 20 records, then go to the next 20 records and keep going until it reaches the end which in this example would be a total of 213 records.


@Future(Callout=true)
public static SyncExternalLeads() {

  Integer recordsPerPage = 20;
  List<Contact> leadsToAdd= new List<Lead>();

  String apiURL = "https://example.com/api/users?page=1";
  HttpResponse apiResponse = doGetRequest( apiURL );
  String jsonResponse = apiResponse.getBody();
  Users userRecords = (Users)JSON.deserialize(userRecords, Users.class);

 for( Integer i = 1; i <= recordsPerPage; i++) {
  Lead l = new Lead();
  l.FirstName = userRecords.data[i].first_name;
  l.LastName = userRecords.data[i].last_name;
  l.Company = userRecords.data[i].company_name;
  l.add(leadsToAdd);
 }

 insert leadsToAdd;
}

I know the above code only gets the first 20 records, but that is what I am trying to figure out how to modify my existing code to make multiple api calls and I would assume it would need to keep appending to the "leadsToAdd" list until all records are added and then do an insert, but not sure how to get there.

JSON Included Below

{
 
  "total_records": 6,
  "total_number_pages": 1,
  "current_page_number": 1,
  "data": [
    {
      "id": 92020,
      "first_name": "John",
      "last_name": "Smith",
      "company_name": "Mason Systems, Inc"
    },
    {
      "id": 92025,
      "first_name": "Bruce",
      "last_name": "Lee",
      "company_name": "Technical Services, Inc."
    }

  ]
}
2
  • We need to know the entire payload structure of the response to provide an informed answer.
    – Adrian Larson
    Commented Aug 27, 2020 at 19:27
  • I have added the JSON returned from the API call @AdrianLarson Commented Aug 27, 2020 at 19:39

1 Answer 1

0

You need to parse the response of the first callout to figure out how many more you have to make.

Integer pageCount = userRecords.total_number_pages;
if (pageCount <= 1) return;
for (Integer i = 0; i < pageCount - 1; i++)
{
    String newEndpoint = 'https://example.com/api/users?page=' + (i + 1);
    // call out again
}
4
  • Should that be a "+" sign instead of an "=" sign in the query string after "page=" @AdrianLarson Commented Aug 27, 2020 at 19:53
  • No. URI Parameters in a query string use name=value pairs.
    – Adrian Larson
    Commented Aug 27, 2020 at 20:00
  • I mean this part: "= (i + 1);" should it not be "+ (i +1); So that way the result is page=2 instead of page==2? Otherwise I get this error: Expression cannot be assigned Commented Aug 27, 2020 at 20:03
  • Yeah missed the shift key I guess.
    – Adrian Larson
    Commented Aug 27, 2020 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .