0

I am having the following code to insert a Case record. When I run the code using workbench I am able to insert a case record. But when I try to call the API from another sandbox I am getting Error :

[{"message":"Unexpected parameter encountered during deserialization: attributes at [line:1, column:16]","errorCode":"JSON_PARSER_ERROR"}]

getAccessToken() is used to get Bearer I don't have any issue with that.

Can anyone point me to what mistake I am doing in this code?

@AuraEnabled(cacheable = true) 
    public static void saveCaseRecOrg1()
    {
        Case newCase = new Case( Description='Test Case');
        String oauthToken = uploadCase.getAccessToken();
        String url ='https://demoinstance1-dev-ed.my.salesforce.com/services/apexrest/myRESTAPI/saveCaseRecOrg2';

        HttpRequest saveCaseReq = new HttpRequest();
        Http http = new Http();
        saveCaseReq .setEndpoint(url);
        saveCaseReq .setMethod('POST');
        saveCaseReq .setHeader('Authorization','Bearer ' + oauthToken);
        saveCaseReq .setHeader('Content-Type', 'application/json;charset=UTF-8');
        saveCaseReq .setBody(JSON.serialize(newCase));
        HttpResponse response = http.send(saveCaseReq );
        Object jsonResponse = (Object) JSON.deserializeUntyped(response.getBody());
        if(response.getStatusCode() == 200){
            System.debug('##### jsonResponse '+jsonResponse );
        }
    }

===================================================== Post method

@HTTPPost 
    global static void saveCaseRecOrg2(Case newCase)
    {
        insert newCase;
    }

1 Answer 1

0

The outer JSON needs to have the request parameter, then the parameter name:

saveCaseReq .setBody(JSON.serialize(
  new Map<String, Object> {
    'request' => new Map<String, Object> {
      'newCase' => newCase
    }
  }
));

See also this answer.

You must log in to answer this question.

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