1

I have a callout method which helps to get an list of objects from another salesforce-organisation. But how can I convert this list into objects on callout's organisation side? And how can I do this with bulkification (many records)? For example, to get 100 entries for 1 request, and then another 100, if they exist (bulkification with pagination).

HttpGet:

@RestResource(urlMapping='/endpoint/*')
global with sharing class Endpoint {

@HttpGet
global static List<Item__c> getItem(){
    RestRequest req = RestContext.request;

    List<Item__c> itemList;
    RestResponse res = new RestResponse();
    RestContext.response = res;

    try{
        itemList = [SELECT Id, Full_Name__c FROM Item__c LIMIT 100];
        res.responseBody = Blob.valueOf(JSON.serialize(itemList));
    } catch(Exception e){
        System.debug('Error:  ' + e.getMessage());
    }
    return itemList;
}
}

Callout:

public class Callout {    
public static String webTokenBody(){
    String requestBody = 'my_web_token_string';
    return request;
}

public class webTok{
    public String webToken{get;set;}    
}

public static HttpRequest createRequestForToken(String service){
    String requestBody = webTokenBody();
    HttpRequest ourRequest = new HttpRequest();
    ourRequest.setBody(requestBody);
    ourRequest.setMethod(service);
    ourRequest.setEndpoint('https://my.salesforce.com/services/oauth2/token');
    return ourRequest;
}

public static HttpRequest createRequestForService(String token, String method){
    HttpRequest finalRequest = new HttpRequest();
    finalRequest.setHeader('Authorization','Bearer ' + token);
    finalRequest.setHeader('Content-Type','application/json');
    finalRequest.setHeader('accept','application/json');
    finalRequest.setMethod(method);
    finalRequest.setEndpoint('https://my.salesforce.com/services/apexrest/endpoint/');
    return finalRequest;
}

public static HttpResponse getCalloutResponseContents() {   
    Http ourHttp = new Http();
    HttpRequest requestForToken = createRequestForToken('GET');
    HttpResponse responseToken = ourHttp.send(requestForToken);
    webTok objAuthenticationInfo = (webTok)JSON.deserialize(responseToken.getbody(), webTok.class);

    if(objAuthenticationInfo.webToken != null){
        HttpRequest requestForService = createRequestForService(objAuthenticationInfo.webToken, 'GET');
        HttpResponse responseService = ourHttp.send(requestForService);
        System.debug('ALL RECORDS:  ' + responseService.getBody());
        return responseService;
    }
}
}
2

You can do that using SOQL offset. Add an attribute called as offset in your SOQL and in your rest service

@HttpGet
global static List<Item__c> getItem(){
    RestRequest req = RestContext.request;
    Integer rowsToSkip=Integer.valueOf( req.requestURI.substring(req.requestURI.lastIndexOf('/')+1));

    List<Item__c> itemList;
    RestResponse res = new RestResponse();
    RestContext.response = res;

    try{
        itemList = [SELECT Id, Full_Name__c FROM Item__c  LIMIT 100 Offset=:rowsToSkip];
        res.responseBody = Blob.valueOf(JSON.serialize(itemList));
    } catch(Exception e){
        System.debug('Error:  ' + e.getMessage());
    }
    return itemList;
}

Then when you are calling provide that offset

finalRequest.setEndpoint('https://my.salesforce.com/services/apexrest/endpoint/200');

The limitation is it works only till 2000 rows. If you want more then the solution get bit complex. It can be done by passing the lastRetrived recordID but as of now offset will do

  • Can we realize that but with ApexPages.StandardSetController to get around limit 2000? How to do that? I worked with it just in VF Controller with buttons and page-refresh. – MichaelLev19 Nov 22 '18 at 9:15
  • Rest webservices method are static, VF controller method are not static and they save things like query cursor in View state(browser)... That helps it to remember the state of records nd call next last method accordingly. Now in webservices that benefit of view state isn't there so it won't be possible on top of my head. – Pranay Jaiswal Nov 22 '18 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.