0

I am using workbench to to test my webservice. I have a Soql Query which I serialize and then format by the website below:

List<Case> casesList = [SELECT ID, status, origin, (SELECT id, parentId, fromAddress, ToAddress, TextBody, htmlBody FROM EmailMessages where ParentId = '5004J000005hszVQAQ') From Case where id = '5004J000005hszVQAQ'];

String str = JSON.serialize(casesList);

Format JSON: https://www.freeformatter.com/json-formatter.html#ad-output

However when paste this into Workbench I get a JSON_PARSER_ERROR, Unexpected parameter encountered during deserialization.

I have already looked at the following but I am unsure how to structure the JSON which is where the issue lies:

Error : JSON request body must be an object

JSON:

{"jsonBodyList":
[
 {
  "attributes": {
     "type": "Case",
     "url": "/services/data/v47.0/sobjects/Case/5004J000005hszVQAQ"
  },
  "Id": "5004J000005hszVQAQ",
  "Status": "Waiting on Customer",
  "Origin": "Community",
  "RecordTypeId": "0124J000000xOr1QAE",
  "EmailMessages": {
     "totalSize": 1,
     "done": true,
     "records": [
        {
           "attributes": {
              "type": "EmailMessage",
              "url": "/services/data/v47.0/sobjects/EmailMessage/02s4J000006FuckQAC"
           },
           "ParentId": "5004J000005hszVQAQ",
           "Id": "02s4J000006FuckQAC",
           "Subject": "Shipping container    [ ref:_00D4Ju6Mt._5004J5hszV:ref ]",
           "FromAddress": "[email protected]",
           "FromName": "someuser",
           "ToAddress": "[email protected]",
           "TextBody": "This is an email\nref:_00D4Ju6Mt._5004J5hszV:ref",
           "HtmlBody": "<html>\n<head>\n\t<title><\/title>\n<\/head>\n<body>This is an email<br><br>ref:_00D4Ju6Mt._5004J5hszV:ref<\/body>\n<\/html>"
        }
     ]
  }
 }
]
}

Wrapper class:

Global class EmailPost {    
    Global ID emailId;
    Global ID parentId;
    Global String subject;
    Global String fromAddress;
    Global String fromName;
    Global String ToAddress;
    Global String TextBody;
    Global String htmlBody;
    Global String caseStatus;
    Global String caseOrigin;

    Global EmailPost(ID emailId, ID parentId, String subject, String fromAddress, String fromName, String ToAddress, String TextBody,
                     String htmlBody, String caseStatus, String caseOrigin){
        this.emailId = emailId;
        this.parentId = parentId;
        this.subject = subject;
        this.fromAddress = fromAddress; 
        this.fromName = fromName;
        this.ToAddress = ToAddress;
        this.TextBody = TextBody;
        this.htmlBody = htmlBody; 
        this.caseStatus = caseStatus;
        this.caseOrigin = caseOrigin;
    }

}

WebService:

@RestResource(urlMapping='/Cases/*')
Global class WebServiceRest {

 @HttpPut
 Global static void putCase(EmailPost e) {
    Case c = new Case();
    c.Id = e.ParentId;
    c.status = e.caseStatus;
    c.Origin = e.caseOrigin;
    upsert c;

    //same email attached to case
    EmailMessage currentEmail = new EmailMessage();
    currentEmail.id = e.emailId;
    currentEmail.fromAddress = e.fromAddress;
    currentEmail.fromName = e.fromName;
    currentEmail.ToAddress = e.ToAddress;
    currentEmail.subject = e.subject;
    currentEmail.TextBody = e.TextBody;
    currentEmail.htmlBody = e.htmlBody;
    currentEmail.ParentId = e.ParentId;
    upsert currentEmail;
  }

I am unsure how to structure my JSON so I no longer get a parser error. Any help appreciated!

2
  • can you add code of putCode method? Commented Feb 4, 2020 at 19:32
  • @PranayJaiswal I have updated the code. I am not doing too much just grabbing the data passed and upserting it
    – M guy
    Commented Feb 4, 2020 at 19:37

0

You must log in to answer this question.

Browse other questions tagged .