I have a rest resource which fetches more than 10000 records. iterates the list using foreach and adds each record to a custom wrapper class list and finally returns the custom wrapper list as response. when I hit this API with post man. It takes more than 4 seconds to get the response. is there any way to improve the performance?
What are some general best practices to improve the performance of a rest api in apex.
please find the code below.
@RestResource(urlMapping='/custom_info/*')
global class custom_Service{
@HttpGet
Webservice static void getAssetListForSelectedUser(){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
customWrapper result;
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, Field_one__c,Field_two__c,Field_three__c, Field_four__c, Field_five__c, Field_six__c Description__c from Asset__c where ownerId = : usr.id];
result = new customWrapper(usr.name, usr.LanguageLocaleKey, 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<innerWrapper> astList{get;set;}
public customWrapper(){}
public customWrapper (String name, String lan, List<Asset__c> assets) {
name = name;
language = lan;
for (Asset__c a : assets) {
astList.add(new innerWrapper(a));
}
}
}
global class inner wrapper {
public String name{get;set;}
public String createdDate{get;set;}
public String serial{get;set;}
public String Field_one__c{get;set;}
public String Field_two__c{get;set;}
public String Field_three__c{get;set;}
public String Field_four__c{get;set;}
public String Field_five__c{get;set;}
public String Field_six__c{get;set;}
public innerWrapper(){}
public innerWrapper(List<Asset__c> assets) {
name = assets.name;
createdDate = assets.createdDate;
serial = assets.serial__c;
fieldOne = assets.Field_one__c;
fieldTwo = assets.Field_two__c;
fieldThree = assets.Field_three__c;
fieldFour = assets.Field_four__c;
fieldFive = assets.Field_five__c;
fieldSix = assets.Field_six__c;
}
}
}
please help.
webservice
when you meanglobal
. This might confuse some developers who read your code later.webservice
means you want to call this method with SOAP instead of REST, but you're using REST-only properties.