+ Start a Discussion
Baz DensonBaz Denson 

handle http post variables with nesting

I am trying to write the following JSON to a custom Object.
 
Body
{
  "events": [
    {
      "id": "EVTESTKA5SS3XP",
      "created_at": "2017-11-23T08:36:52.561Z",
      "resource_type": "mandates",
      "action": "created",
      "links": {
        "mandate": "index_ID_123"
      },
      "details": {
        "origin": "api",
        "cause": "mandate_created",
        "description": "Mandate created via the API."
      },
      "metadata": {}
    }
  ]
}

I have the following code:
 
@RestResource(urlMapping='/gocardless/*')
global with sharing class GoCardlessEndpoints {

    
  /*  
   HttpPost method is used to capture a HttpPost request has been sent to our rest apex class.  
   Used to retrieve data coming in the request body and performing corressponding actions  
  */  
  @HttpPost  
   global static String doPost() {  
     /*  
       RestContext Class - Allows us to access the RestRequest and RestResponse objects in your Apex REST methods.   
       RestRequest class - Allows us to pass request data into our Apex RESTful Web service method.  
       RestResponse class - Allows us to pass or send back response data from our Apex RESTful web service method  
     */  
     //Returns the RestRequest object for our Apex REST method.  
     RestRequest request = RestContext.request;  
     //Returns the RestResponse for our Apex REST method.  
     RestResponse response = RestContext.response;  
     //Access the request body with input data coming in the JSON format  
     String jSONRequestBody=request.requestBody.toString().trim();  
     //Deserializes the input JSON string into an GoCardless_Event__c object  

      ResponseResult e = (ResponseResult)JSON.deserialize(jSONRequestBody, ResponseResult.class);
      
       for(integer i=0; i< e.events.size(); i++) {
     
           GoCardless_Event__c gcevent = new goCardless_Event__c();
           gcevent.name = e.events[i].Id;
           gcevent.created_at__c = e.events[i].created_at;
           gcevent.resource_type__c = e.events[i].resource_type;
           gcevent.action__c = e.events[i].action;
           insert gcevent;
       }
       return 'Done';
	}

    public class ResponseResult {
    
        public Event[] events;

    }
    public class Event {
        public String id, resource_type,action;
        public datetime created_at;
		          
    }


}

I have been able to capture the event variables up to links, but how do I access the nested structures?

Thanks

Barry​
Best Answer chosen by Baz Denson
{!Piyush_soni__c}{!Piyush_soni__c}
hi barry, update your class with below changes

@RestResource(urlMapping='/gocardless/*')
global with sharing class GoCardlessEndpoints {

    
  /*  
   HttpPost method is used to capture a HttpPost request has been sent to our rest apex class.  
   Used to retrieve data coming in the request body and performing corressponding actions  
  */  
  @HttpPost  
   global static String doPost() {  
     /*  
       RestContext Class - Allows us to access the RestRequest and RestResponse objects in your Apex REST methods.   
       RestRequest class - Allows us to pass request data into our Apex RESTful Web service method.  
       RestResponse class - Allows us to pass or send back response data from our Apex RESTful web service method  
     */  
     //Returns the RestRequest object for our Apex REST method.  
     RestRequest request = RestContext.request;  
     //Returns the RestResponse for our Apex REST method.  
     RestResponse response = RestContext.response;  
     //Access the request body with input data coming in the JSON format  
     String jSONRequestBody=request.requestBody.toString().trim();  
     //Deserializes the input JSON string into an GoCardless_Event__c object  

      ResponseResult e = (ResponseResult)JSON.deserialize(jSONRequestBody, ResponseResult.class);
      
       for(integer i=0; i< e.events.size(); i++) {
     
           GoCardless_Event__c gcevent = new goCardless_Event__c();
           gcevent.name = e.events[i].Id;
           gcevent.created_at__c = e.events[i].created_at;
           gcevent.resource_type__c = e.events[i].resource_type;
           gcevent.action__c = e.events[i].action;
           gcevent.yourFieldName__c = e.events[i].links.mandate;
           insert gcevent;
       }
       return 'Done';
    }

    public class ResponseResult {
    
        public Event[] events;

    }
    public class Event {
        public String id, resource_type,action;
        public datetime created_at;
        public objLinks links;
                  
    }
    
    public class objLinks{
       string mandate;
    }



}



i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks
http://sfdcmonkey.com

All Answers

{!Piyush_soni__c}{!Piyush_soni__c}
hi barry, update your class with below changes

@RestResource(urlMapping='/gocardless/*')
global with sharing class GoCardlessEndpoints {

    
  /*  
   HttpPost method is used to capture a HttpPost request has been sent to our rest apex class.  
   Used to retrieve data coming in the request body and performing corressponding actions  
  */  
  @HttpPost  
   global static String doPost() {  
     /*  
       RestContext Class - Allows us to access the RestRequest and RestResponse objects in your Apex REST methods.   
       RestRequest class - Allows us to pass request data into our Apex RESTful Web service method.  
       RestResponse class - Allows us to pass or send back response data from our Apex RESTful web service method  
     */  
     //Returns the RestRequest object for our Apex REST method.  
     RestRequest request = RestContext.request;  
     //Returns the RestResponse for our Apex REST method.  
     RestResponse response = RestContext.response;  
     //Access the request body with input data coming in the JSON format  
     String jSONRequestBody=request.requestBody.toString().trim();  
     //Deserializes the input JSON string into an GoCardless_Event__c object  

      ResponseResult e = (ResponseResult)JSON.deserialize(jSONRequestBody, ResponseResult.class);
      
       for(integer i=0; i< e.events.size(); i++) {
     
           GoCardless_Event__c gcevent = new goCardless_Event__c();
           gcevent.name = e.events[i].Id;
           gcevent.created_at__c = e.events[i].created_at;
           gcevent.resource_type__c = e.events[i].resource_type;
           gcevent.action__c = e.events[i].action;
           gcevent.yourFieldName__c = e.events[i].links.mandate;
           insert gcevent;
       }
       return 'Done';
    }

    public class ResponseResult {
    
        public Event[] events;

    }
    public class Event {
        public String id, resource_type,action;
        public datetime created_at;
        public objLinks links;
                  
    }
    
    public class objLinks{
       string mandate;
    }



}



i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks
http://sfdcmonkey.com
This was selected as the best answer
Baz DensonBaz Denson
Thanks Piyush

I'm sure I had tried somehting along those lines already but it wouldn't work!
{!Piyush_soni__c}{!Piyush_soni__c}
barry, i have checked above code in my dev org and it works for me. I have just put simple debug to check links.mandate value and m getting the result.
User-added image
Thanks
 
Baz DensonBaz Denson
Yeah, sorry I meant that you original solution had worked but I thought I had already tried that, or something similar before.