0

I have the following piece of code.

@RestResource(urlMapping='/Update')
global with sharing class OpportunityUpdate {
@HttpPost
global static void updateOpportunity() { 

    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;

    Blob body = req.requestBody;
    String requestString = body.toString();
    AreaRequestWrapper areaRequestRecord = parse(requestString);
    System.debug('***********'+String.valueOf(areaRequestRecord));
}

public class RequestWrapper{
public String SourceSystem; 
public Integer MessageVersion;  
public cls_messages messages;
}
class cls_messages {
    public cls_Transaction[] Transactions;
}
class cls_Transaction {
    public cls_MetaData MetaData;
    public String OId;  
    public String TId;  
    public boolean IsTransactionComplete;
    public String PId;  
}
class cls_MetaData {
    public String Guid; 
    public String MessageOperation; 
    public String Country;  
}
public static RequestWrapper parse(String json){
    return (RequestWrapper) System.JSON.deserialize(json,RequestWrapper.class);
}

I have generated above AccountWrapper using JSON2Apex from the following JSON

 { 
    "SourceSystem":"Sys",
    "MessageVersion":1,
    "messages":{ 
       "Transaction":[ 
          { 
             "MetaData":{ 
                "Guid":"6666-7777-3333",
                "MessageOperation":"edit",
                "Country":"CON"
             },
             "OId":"0361k006874282AA",
             "TId":"134207",
             "IsTransactionComplete":true,
             "PId":"54601"
          }
       ]
    }
 }

My problem here is that, when I parse using parse(String) method, it is giving null for the inner class values. The above System.debug prints the following

[MessageVersion=1, SourceSystem=Area, messages=cls_messages:[Transactions=null]]

I am not sure why Transactions is coming as null.

I went through the below link as well. But in my case I do not have nested inner classes.

Getting null for inner classes in complex JSON deserialization

1 Answer 1

4

Your JSON doesn't match your data structure. In Apex, you have

public cls_Transaction[] Transactions;

in the plural, but your JSON has

"messages":{ 
   "Transaction":[ 
      { 

in the singular.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .