Skip to main content
All CollectionsGeneral questionsSection heading
How to use the getPage() method to retrieve result rows of multiple pages
How to use the getPage() method to retrieve result rows of multiple pages

getPage, getPageOffset

Jason Liang avatar
Written by Jason Liang
Updated over 3 years ago

The getPage() method is often called to retrieve a list of objects, such as user items in user service, entities in global entity service, user entity service, custom entity service (named getEntityPage in custom entity service).

You may have noticed, the max number of rows of items return from getPage() method is limited to 100. In the case that these items are greater than 100, you will have to give an additional call to get the rest items.

  • The simplest way to achieve this is to combine it with the getPageOffset() method, whenever the returned data moreAfter is true, you can continue to call the next offset page. See the example below for getting all the user items via a cloud code script:

"use strict";

function main() {

bridge.logDebugJson("Script inputs", data);
var response = {};
var maxperpage = data.maxperpage;
var context = {
"pagination": {
"rowsPerPage": maxperpage,
"pageNumber": 1
},
"searchCriteria": {},
"sortCriteria": {
"playerId": 1
}
};
var userProxy = bridge.getUserServiceProxy();

var aPlayerIds=[];
var offsetPageNumber = 0;

var firstPageResult = userProxy.sysGetPage( context );
if (firstPageResult.status == 200) {
firstPageResult.data.results.items.forEach(item=>aPlayerIds.push(item.playerId));
var firstPageContext = firstPageResult.data.context; //if using this conte

//using moreAfter to determine if calling sysGetPageOffset
var loopflag = firstPageResult.data.results.moreAfter;

while (loopflag){
offsetPageNumber ++;
var nextPageResult = userProxy.sysGetPageOffset(firstPageContext, offsetPageNumber);
nextPageResult.data.results.items.forEach(item=>aPlayerIds.push(item.playerId));
loopflag = nextPageResult.data.results.moreAfter;
}
}

response = {
"aPlayerIds": aPlayerIds,
"itemsCount": aPlayerIds.length
};

return response;

}

main();
  • Another alternative will be to keep calling sysGetPage() for the next page instead of calling sysGetPageOffset(), as long as changing the pageNumber parameter inside the passed context, see the code snippet below:

"use strict";

function main() {

bridge.logDebugJson("Script inputs", data);
var response = {};
//use pagination to determine if need to get next page
var maxperpage = data.maxperpage;
var pageNumber = 1;
var aPlayerIds = [];
var context = {
"pagination": {
"rowsPerPage": maxperpage,
"pageNumber": pageNumber
},
"searchCriteria": {},
"sortCriteria": {
"playerId": 1
}
};
var userProxy = bridge.getUserServiceProxy();

var firstPageResult = userProxy.sysGetPage( context );
var count = firstPageResult.data.results.count;
if(count>0){
firstPageResult.data.results.items.forEach(item=>aPlayerIds.push(item.playerId));
//if return items count great than maxperpage will get into belows for loop to call next page, until scan all
var remainder = 0;
var numberpage = 0;
remainder = count % maxperpage;
numberpage = (count-remainder)/maxperpage;

for (var n = 0; n < numberpage; n++ ){

pageNumber ++;

context.pagination.pageNumber = pageNumber;

var nextPageResult = userProxy.sysGetPage( context );
if (nextPageResult.status == 200) {
nextPageResult.data.results.items.forEach(item=>aPlayerIds.push(item.playerId));
}
}

}
response = {
"aPlayerIds": aPlayerIds,
"itemsCount": aPlayerIds.length
};

return response;

}

main();
Did this answer your question?