Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Populate StandardSetController from API
public ApexPages.StandardSetController itemsSetController{
get{
if(itemsSetController == null) {
Item__c[] items = new Item__c[]{};
HttpRequest req = new HttpRequest();
req.setEndpoint('https://'+URL.getSalesforceBaseUrl().getHost()+'/services/data/v35.0/query/?q=SELECT+Id,Name+FROM+Item__c+WHERE+'where_clause'+ORDER+BY+Name+ASC');
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer '+ userInfo.getsessionId());
Boolean done = false;
do{ JSONParser parser = JSON.createParser(new Http().send(req).getBody());
while(parser.nextToken() != null){
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText()=='totalSize'){
parser.nextToken();
totalSize = already_added.isEmpty()||!String.isBlank(search_string) ? parser.getIntegerValue() : parser.getIntegerValue()-already_added.size();
}
if(parser.getCurrentToken() == JSONToken.START_ARRAY)
while(parser.nextToken() != null)
if(parser.getCurrentToken() == JSONToken.START_OBJECT && items.size()<50)
items.add((Item__c)parser.readValueAs(Item__c.class));
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText()=='done'){
parser.nextToken();
done = parser.getBooleanValue();
}
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText()=='nextRecordsUrl'){
parser.nextToken();
req.setEndpoint('https://'+URL.getSalesforceBaseUrl().getHost()+parser.getText());
}
}
}while(!done);
//populate StandardSetController
itemsSetController = new ApexPages.StandardSetController(items);
itemsSetController.setPageSize(5);
//set totalPages
Integer count=1;
if(itemsSetController.getHasNext()){
while(itemsSetController.getHasNext()){
count++;
itemsSetController.next();
}
itemsSetController.first();
}
totalPages = count;
}
return itemsSetController;
}
set;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment