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."
}
]
}