Pagination on a REST API call (Code)
Objectif
Connect a microsoft REST API (HRTB) that returns only a fixed number of records at a time.
Do pagination
Code
Step 1 : Get the number of records
Step 2 : Loop and extract x records each time
The only trick there is to find which field on the table you are querying can be used for pagination.
In my case I'm using Id. The Id field has unique and incremental values.
try {
var sm = new sn_ws.RESTMessageV2();
var basic_url = "the URL to get all your records without a filter";
sm.setEndpoint(basic_url);
var data = call(sm);
var user = new GlideRecord('sys_user');
var cost_center = new GlideRecord('cmn_cost_center');
var dep = new GlideRecord('cmn_department');
var bu = new GlideRecord('u_business_unit');
var update = false;
var total = data.length;
// Define how many records at a time you want to extract
var limit_rest = 5000;
var x = 1;
var midServerName = new GlideRecord('ecc_agent');
// Make sure the result contain data
if(!isNaN(parseInt(total))){
// Pagination extracting 5000 records a time at most
while(x<total){
// get all user Ids between x and x + 5000
sm.setEndpoint(basic_url+"&filter=Id%20*Lt%20"+(x+limit_rest)+"%20and%20Id%20Gt*%20"+x);
sm.setBasicAuth("xxxxxxxxxxxxx","xxxxxxxxxxxxxxxxxxxxxxxxxx");
data = call(sm);
x += limit_rest;
for (var i=0; i<data.length; i++){
// DO STUFF
}
}
}
}
catch(ex) {
}
// This function perform the API call and return the data.
function call(sm){
var response = sm.execute();
var responseBody = JSON.parse(response.getBody());
var httpStatus = response.getStatusCode();
var data = responseBody.Data;
var headers = sm.getRequestHeaders();
var getHeaders = "";
for(var h in headers){
getHeaders += h+":"+headers[h]+"\n";
}
return data;
}
https://www.servicenow.com/community/developer-articles/pagination-on-a-rest-api-call-code/ta-p/2322901