0

I need to set a success response in a String. How can I serialize a response in string?

3
  • 4
    Where exactly are you facing issue? Add some details in your question please. Mar 1, 2018 at 6:45
  • I need to create a response json with string 'case created with caseId
    – ADITI
    Mar 1, 2018 at 6:49
  • Please edit your post to be more clear about observed and desired behavior, and how they diverge.
    – Adrian Larson
    Mar 1, 2018 at 14:18

1 Answer 1

2

Apex takes care of the serialization if you just return the string from your method.

From the documentation:

If the method returns void, then Apex REST returns the response in the responseBody property. If the method returns a value, then Apex REST serializes the return value as the response.

So change your method signature to

@HttpPost
global static String addCases(){
 try
  {
  RestContext.response.statusCode = 200;
  return 'successfully inserted'+cases.id;
 }
 catch(Exception e)
 {
  return 'Error:case insertion failure';
 }
}

You can send different status codes based on your logic.

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