0

I am making a POST callout from salesforce to another service and case is one of the objects of JSON which i am sending (see below)

"case": {
   "caseId": "00000212",
   "status":"",
   "statusReason":""
}

Same is sent in response with updated "status" and "statusReason" values. Since CASE cannot be used as a property for JSONtoApex class i am replacing it with "caseObj". So the response should be

"caseObj": {
       "caseId": "00000212",
       "status":"",
       "statusReason":""
    }

For some reason i am seeing only "status" and "statusReason" but not the "caseId" in the response. It is working fine when i call the same service with same payload through POSTMAN, i am able to see the "caseId". Looks like the problem is within salesforce, i also tried replacing "caseId" with some other property but still didn't work.

Response in Salesforce (highlighted in red): enter image description here

Response in POSTMAN

enter image description here

UPDATED SECTION

CalloutHelper

public class CalloutHelper{
  public static String postData(String body){
    HttpRequest rq = new HttpRequest();
    rq.setEndpoint('myendpoint');
    rq.setMethod('POST');
    rq.setbody(body);

    Http http = new Http();
    HttpResponse rs = http.send(rq);
    if(rs.getStatusCode() == 200){
     return rs.getBody();
    } else {
     return 'That did not work';  
    }
    return null;
  }
}

JSONResponseClass

public class JSONResponseClass {

  public responseBody{
    @AuraEnabled
    public String correspondenceId;
    @AuraEnabled
    public cls_recipients[] recipients;
  }

  public class cls_recipients {
    @AuraEnabled
    public cls_case caseObj;
    @AuraEnabled
    public String firstName;
    @AuraEnabled
    public String lastName;
    @AuraEnabled
    public String email;
    @AuraEnabled
    public String fax;
    @AuraEnabled
    public cls_address address;
  }

  public class cls_case {
    @AuraEnabled
    public String caseId;
    @AuraEnabled
    public String status;
    @AuraEnabled
    public String statusReason;
  }

public class cls_address {
    @AuraEnabled
    public String street;
    @AuraEnabled
    public String city;
    @AuraEnabled
    public String state;
  }

  public static responseBody sendData(String body){
    String returnValue = calloutHelper.postData(body);
    returnValue = returnvalue.replace('case','caseObj');
    return (responseBody) System.JSON.deserialize(returnValue, responseBody.class);
  }
}

I am calling JSONResponseClass in a lightning component and sending "body" as a param to Apex controller. From the images above, first image is what the response received captured through chrome (console tab) using JSON.stringify(response).

Response Body Before Serialization

enter image description here

Thank you in advance.

closed as off-topic by Jayant Das, Basti, Pranay Jaiswal, Vijay Ganji, Mohith Shrivastava Jun 10 at 13:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Jayant Das, Basti, Pranay Jaiswal, Vijay Ganji, Mohith Shrivastava
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Are you using any apex wrapper? – Dhanik Lal Sahni Jun 4 at 15:01
  • Please update your question to include the actual code you are using including an object wrappers, deserialization methods and how you are generating the output that you have shown us when. – gNerb Jun 4 at 15:11
  • @gNerb Apologies for not providing the code. Please see the updated section of the question. – SunnyG Jun 4 at 15:57
  • @DhanikLalSahni yes – SunnyG Jun 4 at 15:58
  • provided JSON.stringify(response).is before serialization? Are you getting caseId before serialization? – Dhanik Lal Sahni Jun 4 at 16:26
0

Replace response body before serialization like below. It will change case object to caseObj which you have refereed in class.

jsonString.replace('"case":', '"caseObj":');

After this you can de-serialize and it will work.

  • I did that.. i have not included here in the code. Please see the Response In Salesforce image in the question. you will see "caseObj" instead of "case" – SunnyG Jun 4 at 17:51
  • returnValue = returnvalue.replace('case','caseObj'); looks like case in caseId is also replacing with caseObj so it is not de-serializing. use code which i have added in answer to replace. – Dhanik Lal Sahni Jun 4 at 18:22
  • Ah!! i completely forgot that "caseId" also has "case" in it and that is being replaced with caseObj. Thanks, that worked! – SunnyG Jun 4 at 18:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.