I have rest resource exposed to a external system. It is a simple rest api where i get a single parameter from the URL and query a custom object based on that param and return the results in a custom wrapper class.
This custom object has more that 150000 records. now the external system is asking for pagination. Please let me know how do i achieve this.
I tried using StandardSetController and i got an error saying No Visualforce context has been established!
please find the sample code below without pagination...
@RestResource(urlMapping='/custom_info/*')
global class custom_Service{
@HttpGet
Webservice static void getAssetListForSelectedUser(){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
customWrapper result = new customWrapper();
String federationId=req.params.get('recid');
User usr = new User();
usr = [Select Id, Name, LanguageLocaleKey from User where FederationIdentifier = :federationId];
List<Asset__c> assets = [Select Id, Name, createddate, serial__c, Description__c from Asset__c where ownerId = : usr.id Limit 30000];
result.name = usr.name;
result.name = usr.LanguageLocaleKey;
result.assets = assets;
res.statusCode = 200;
res.responseBody = Blob.valueOf(JSON.serialize(result));
return;
}
global class customWrapper {
public String name{get;set;}
public String language{get;set;}
public List<Asset__c> assets{get;set;}
public customWrapper(){}
}
}
Below is the sample code with pagination using standardsetcontroller. For pagination i receive 2 more params offset and limit. But i am getting No Visualforce context has been established!
error.
@RestResource(urlMapping='/custom_info/*')
global class custom_Service{
@HttpGet
Webservice static void getAssetListForSelectedUser(){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
customWrapper result = new customWrapper();
ApexPages.StandardSetController setController = null;
String federationId=req.params.get('recid');
String pageSize = req.params.get('limit');
String pageNumber = req.params.get('offset');
User usr = new User();
usr = [Select Id, Name, LanguageLocaleKey from User where FederationIdentifier = :federationId];
List<Asset__c> assets = [Select Id, Name, createddate, serial__c, Description__c from Asset__c where ownerId = : usr.id];
setController = new ApexPages.StandardSetController(assets);
if((pageSize != '' || pageSize != null) && (pageNumber != '' || pageNumber != null)) {
setController.setPageSize(Integer.valueOf(pageSize));
setController.setPageNumber(Integer.valueOf(pageNumber));
}else {
setController.setPageSize(1000);
setController.setPageNumber(1);
}
result = new customWrapper(usr.name, usr.LanguageLocaleKey, setController);
result.name = usr.name;
result.name = usr.LanguageLocaleKey;
result.assets = assets;
res.statusCode = 200;
res.responseBody = Blob.valueOf(JSON.serialize(result));
return;
}
global class customWrapper extends PaginatedResult {
public String name{get;set;}
public String language{get;set;}
public List<Asset__c> assets{get;set;}
public customWrapper(){}
public customWrapper (String name, String lan, ApexPages.StandardSetController c) {
super(c);
name = name;
language = lan;
for (Asset__c a : (Asset__c[])c.getRecords()) {
assets.add(a);
}
}
}
public virtual class PaginatedResult {
public Integer page;
public Integer pageSize;
public Integer pageCount;
public Integer total;
public PaginatedResult() {}
public PaginatedResult(ApexPages.StandardSetController c) {
page = c.getPageNumber();
total = c.getResultSize();
calcPagination(c.getPageSize());
}
public void calcPagination(Integer pageSize) {
this.pageSize = pageSize;
pageCount = total / pageSize;
pageCount = (Integer)(Math.floor(pageCount) + ((Math.mod(total, pageSize) > 0) ? 1 : 0));
}
}
}
Please help....