+ Start a Discussion
SFDCMattSFDCMatt 

Custom Apex REST service w/ batchable POST / PUT?

In reviewing the webinar here on Apex REST API:

 

http://www.youtube.com/watch?v=uSK06phNEp4

 

At 43:40, Sandeep mentions bulkifying the Apex REST classes to accept "a list of sObjects". Does anyone have any documentation on exactly whether or not this is supported in Apex REST services? All the sample code I can find for POST or PUT operate on requests with a single record. I've also seen several posts on StackExchange where people were struggling with this.

 

Is it available at the moment? Can I build a custom Apex REST service that takes in multiple new records in a POST and create those records in bulk?

Henry AkpalaHenry Akpala

Yes you can send list of records to Apex REST API.  The trick is to create a list as a json message in the structure of the object that you want to work with and pass it to the service just like below...  The list is only limited by the size of the API size governor limit.  Below is a fragment of code using a APEX test class to call the APEX REST service.  

+++++++++++++++

 RestRequest re = new RestRequest(); 

        RestResponse rp = new RestResponse();

        re.addHeader('httpMethod', 'POST');  

        re.addHeader('requestURI',ENDPOINTURL);  

 //arguement for param

        String param1 = '{"listOfRecords":[{"action":"ClericalReview","resultList":[{"score":"89","sfdcId":"003U000000OiRWjIAN"}],"searchSFDCId":"003U000000OiRWjIAN"},{"action":"ClericalReview","resultList":[{"score":"89","sfdcId":"003U000000OiRWkIAN"}],"searchSFDCId":"003U000000OiRWkIAN"}]}';

          re.addParameter('Param',param1);

        RestContext.request = re;

        RestContext.response = rp;

result = JDRFInitiateServiceClass.doPost();

++++++++++++++++++++++ 

 

from within the APEX Rest Service

The "JDRFInitiatePostCriteriaClass" is a apex object with fields define, and the code fragment below shows how to transform the json message to an object in APEX.

++++++++++

@HttpPost

        global static String doPost(){

            

            RestRequest req = RestContext.request;

            RestResponse res = RestContext.response;      

            //check if there is a data for "Param" params ketset.

             

            if (req.params.containsKey('Param')){

 

        List<JDRFInitiatePostCriteriaClass> y = new List<JDRFInitiatePostCriteriaClass>(); 

        JDRFInitiateListOfRecords  x = new JDRFInitiateListOfRecords ();

        x = (JDRFInitiateListOfRecords)JSON.deserialize( req.params.get('Param') , JDRFInitiateListOfRecords.class);

        y = x.listOfRecords;

       }

}

++++++++++

 

 

++++++++++++++++++++++

// Object with fields for JDRFInitiatePostCriteriaClass

public with sharing class JDRFInitiatePostCriteriaClass {

        

    //variables to hold the parent and the child details of the Post data

    public String SearchSFDCId; 

    public String action;

    public String Initiatemdmid;

    public List<PostRequestChildRecords> ResultList;

    public JDRFInitiatePostCriteriaClass(){

    

    }

    public class PostRequestChildRecords{

    

        public String SFDCId;   

        public String score;

        public String initiatemdmid;        

        public PostRequestChildRecords(){

        

        }

 }

}

++++++++++++++++++++++++++

//List of  JDRFInitiatePostCriteriaClass

 

public with sharing class JDRFInitiateListOfRecords {

public List<JDRFInitiatePostCriteriaClass> listOfRecords;

    

}

 

++++++++++++++++++++++++++++++++

Hope this helps

Regards

-Henry