0

I am trying to create some records using post method but it throws an exception as below:

JSON_PARSER_EXCEPTION "Unexpected parameter encountered during deserialization: JSON Body:

{
   "DummyRequest":{
      "HeaderData":{
         "RecordTypeName":"abc"
      },
      "Cpdt":[
         {
            "Item":"48262"
         }
      ],
      "CasoDt":{
         "CaseObj":""
      }
   }
}

Apex Class:

@RestResource(urlMapping='/CaseRecCreation/*')
global without sharing class APICreatRec{

global cls_DummyRequest DummyRequest;
    global class cls_DummyRequest {
        public cls_HeaderData HeaderData;
        public cls_Cpdt[] Cpdt;
        public cls_CasoDt CasoDt;
    }
    global class cls_HeaderData {
        public String RecordTypeName;
    }
    global class cls_Cpdt {
        public String Item;
    }
    global class cls_CasoDt {
        public String CaseObj;
    }

    @HttpPost
    global static void CreateOrderReq(cls_DummyRequest cObj)  { 
        Case orc= new Case();
        }
}

1 Answer 1

3

So, what's going on is that this statement

global static void CreateOrderReq(cls_DummyRequest cObj)

is going to automatically deserialize the JSON into cObj per the type cls_DumyRequest

But the incoming JSON is:

{
   "DummyRequest":{
      "HeaderData":{
         "RecordTypeName":"abc"
      },
      "Cpdt":[
         {
            "Item":"48262"
         }
      ],
      "CasoDt":{
         "CaseObj":""
      }
   }
}

Per the SFDC Apex REST doc , when you use the implicit deserialization argument, the top level property of your incoming JSON object must agree with the argument name

Since the top level property for your JSON is DummyRequest, then the POST method's argument must be named the same:

global static void CreateOrderReq(cls_DummyRequest DummyRequest)

and you should remove the declaration in the class:

global cls_DummyRequest DummyRequest;  // remove this line

this leaves you with:

@RestResource(urlMapping='/CaseRecCreation/*')
global without sharing class APICreatRec{

    global class cls_DummyRequest {
        cls_HeaderData HeaderData;
        cls_Cpdt[] Cpdt;
        cls_CasoDt CasoDt;
    }
    class cls_HeaderData {
        String RecordTypeName;
    }
    class cls_Cpdt {
        String Item;
    }
    class cls_CasoDt {
        String CaseObj;
    }

    @HttpPost
    global static void CreateOrderReq(cls_DummyRequest DummyRequest)  {
        // local variable DummyRequest has the deserialized JSON 
        Case orc= new Case();
        }
}

Note only the implicit deserialization class cls_DummyRequest need be declared global. Everything else can be private. I tested this in my Trailhead org.

3
  • this is how my class looks like now but I am still receiving the same error. global class Payload { cls_DummyRequest DummyRequest; } global class cls_DummyRequest { public cls_HeaderData HeaderData; public cls_Cpdt[] Cpdt; public cls_CasoDt CasoDt; } global class cls_HeaderData { } global class cls_Cpdt { } global class cls_CasoDt { } @HttpPost global static void CreateOrderReq(Payload cObj) { Case orc= new Case(); }
    – SFDCKASH
    Nov 27, 2021 at 4:59
  • i made a mistake and corrected it. see updated post
    – cropredy
    Nov 28, 2021 at 0:44
  • Thanks, it works now.
    – SFDCKASH
    Nov 29, 2021 at 8:51

You must log in to answer this question.

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