1

How to get the CaseNumber field in the response of the http POST method, please help

Code:

 @HttpPost
    global static string dopost() {        
              RestRequest req = RestContext.request;
              RestResponse res = RestContext.response;
              Case tkt= (Case)JSON.deserialize(req.requestBody.toString(),Case.class);
              system.debug(tkt);
              try {              
                insert tkt;
              }
              catch(DmlException e) {
             return 'The following exception has occurred: ' + e.getMessage();
        }              
              return JSON.serialize(tkt); 
    }

Response :

"{\"attributes\":
{\"type\":\"Case\",\"url\":\"/services/data/v37.0/sobjects/Case/5002800000S
zQ7QAAV\"},\"Status\":\"New\",\"Origin\":\"Phone\",\"Id\":\"5002800000SzQ7Q
AAV\",\"Subject\":\"New Case 001\",\"Reason\":\"Deviation\"}"
6
  • As long as you do not share your code snippet it will be hard for anyone to help you Oct 13, 2016 at 6:22
  • Added @Mohith Shrivastava
    – B1 sfdc
    Oct 13, 2016 at 6:26
  • Are we able to remove the escape character "\" from the Response : "{\"attributes\":{\"type\":\"Case\",\"url\":\"/services/data/v37.0/sobjects/Case/5002800000SziK7AAJ\"},\"Id\":\"5002800000SziK7AAJ\",\"CaseNumber\":\"00001077\",\"Subject\":\"aslkdjfkjsd323232\",\"Status\":\"New\",\"Reason\":\"Deviation\",\"Origin\":\"Phone\"}"
    – B1 sfdc
    Oct 13, 2016 at 6:44
  • If you follow second approach of returning case instead of string ,the escape character won't appesr Oct 13, 2016 at 10:48
  • Am getting this error : Error: Compile Error: unexpected token: 'dopost' at line 14 column 23, when i tried with the 2nd code.
    – B1 sfdc
    Oct 13, 2016 at 11:46

1 Answer 1

1

You will have to query back the Case Number and return the object like below

@HttpPost
global static string dopost() {        
          RestRequest req = RestContext.request;
          RestResponse res = RestContext.response;
          Case tkt= (Case)JSON.deserialize(req.requestBody.toString(),Case.class);
          system.debug(tkt);
          try {              
            insert tkt;
          }
          catch(DmlException e) {
         return 'The following exception has occurred: ' + e.getMessage();
    }       
        Case insertedCase = [Select Id,CaseNumber,Phone,Id,Status,Subject from Case Where Id =:tkt.Id];       
          return JSON.serialize(insertedCase); 
}

Also on another note ,you do not have to return a string ,instead you can make the return object as Case and platform will automatically take care of deserialising it for you

@HttpPost
global static Case  dopost() {        
          RestRequest req = RestContext.request;
          RestResponse res = RestContext.response;
          Case tkt= (Case)JSON.deserialize(req.requestBody.toString(),Case.class);
          system.debug(tkt);
          try {              
            insert tkt;
          }
          catch(DmlException e) {
         return 'The following exception has occurred: ' + e.getMessage();
    }       
        Case insertedCase = [Select Id,CaseNumber,Phone,Id,Status,Subject from Case Where Id =:tkt.Id];       
          return insertedCase; 
}
2
  • No need for the temporary: return [SELECT CaseNumber... FROM Case WHERE Id = :tck.Id]; works just as well.
    – sfdcfox
    Oct 13, 2016 at 6:45
  • :@Mohith Shrivastava, @sfdcfox : Are we able to remove the escape character "\" from the Response : "{\"attributes\":{\"type\":\"Case\",\"url\":\"/services/data/v37.0/sobjects/Case/5002800000SziK7AAJ\"},\"Id\":\"5002800000SziK7AAJ\",\"CaseNumber\":\"00001077\",\"Subject\":\"aslkdjfkjsd323232\",\"Status\":\"New\",\"Reason\":\"Deviation\",\"Origin\":\"Phone\"}"
    – B1 sfdc
    Oct 13, 2016 at 6:48

You must log in to answer this question.

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