0

I have defined a global class to use as Data Transfer Object in a REST service that I am building but I am unable to pass the data to it. I can see that the data in requestBody is passed in properly but JSON.serializer does not parse it properly. The first three lines are the code. The second set is the debug logs and I have included the class definition at the bottom. I know this should work and must be a result of a small detail that I am overlooking.

        String body = restContext.request.requestBody.toString();
        System.debug('printed restContext.request is ' +  body);
        System.debug('printed value  is ' +  JSON.deserialize(restContext.request.requestBody.toString(), RESTEODRequest.class));

Debug logs

13:47:19.0 (3784964)|USER_DEBUG|[7]|DEBUG|printed restContext.request is {"RESTEODRequest": {"entityId": "a0A1C00000mE7hA", "methodName": "NOP", "isContinued": false}}
13:47:19.0 (4559083)|USER_DEBUG|[8]|DEBUG|printed value  is RESTEODRequest:[entityId=null, isContinued=null, methodName=null]

class definition

  global class RESTEODRequest {
    public String entityId { get; set; }
    public String methodName { get; set; }
    public Boolean isContinued { get; set; }
  • 3
    Not adding this as an answer, since I'm not positive, but don't you need to cast it as your class? e.g. (RESTEODRequest)JSON.deserialize(restContext.request.requestBody.toString(), RESTEODRequest.class)); – caleb.breckon Apr 10 '18 at 18:02
  • I would need to cast it if I wanted to assign to a variable of that type. Since I'm only debugging it, it should be fine here. – Jorjani Apr 10 '18 at 18:41
  • Try json2apex.herokuapp.com to generate JSON Classes. – Kealsalas Apr 11 '18 at 0:31
6

It is looking for the RESTEODRequest and you don't have it in your object, leaving it in this format for execute anon

String test = '{"RESTEODRequest": {"entityId": "a0A1C00000mE7hA", "methodName": "NOP", "isContinued": false}}';

RESTRequest result = new RESTRequest();
result = (RESTRequest)System.JSON.deserialize(test, RESTRequest.class);
system.debug(result);

public class RESTRequest
{
    RESTEODRequestData RESTEODRequest {get;set;}
}

public class RESTEODRequestData 
{
    public String entityId { get; set; }
    public String methodName { get; set; }
    public Boolean isContinued { get; set; }
}

12:12:10:003 USER_DEBUG [5]|DEBUG|RESTEODRequest:[RESTEODRequest=RESTEODRequestData:[entityId=a0A1C00000mE7hA, isContinued=false, methodName=NOP]]
  • @Jorjani if this solved your question can you accept it as an answer? – EricSSH Apr 12 '18 at 15:28

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.