I need to set a success response in a String. How can I serialize a response in string?
-
4Where exactly are you facing issue? Add some details in your question please.– Santanu HalderMar 1, 2018 at 6:45
-
I need to create a response json with string 'case created with caseId– ADITIMar 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
Add a comment
|
1 Answer
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.