2

I am new to apex rest . I am trying to expose an apex class to an external system to do post operation in salesforce. I am a bit confused with the response codes and response message which I need to send back to external system. I was referring to this link https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_restresponse.htm#apex_System_RestResponse_statusCode . This gives some standard error codes. I have couple of questions.

  1. Do I need to write some custom logic in my apex rest class to send back the response code or salesforce will automatically determine which response code and message needs to be sent back in response?
  2. Can I also send back any custom response code and message as well . If yes, how to do that? Any sample code would be helpful

2 Answers 2

3

Do I need to write some custom logic in my apex rest class to send back the response code or salesforce will automatically determine which response code and message needs to be sent back in response?

By default, Salesforce chooses a status code for you. This will be 200 for successful transactions, 400 if the request is invalid, etc. By the time your code has started running, Salesforce has already gone through all the steps of authorization and verifying the request meets basic requirements for a well-formatted request.

Can I also send back any custom response code and message as well . If yes, how to do that? Any sample code would be helpful

To set a status code, use:

RestContext.response.statusCode = statusCode; // Some integer

Only use the status codes in the list. Any other status code will result in a 500 error message.

To return a message, you can do so in several ways.

First, make sure you also set a content type if returning any content:

RestContext.response.headers.put('Content-Type','text/plain');

You can return a custom class with properties, which will automatically be converted to XML or JSON depending on the Accept request header. Alternatively, you can set RestContext.response.responseBody to a blob containing any kind of data you desire, even binary data.

Do not return a value and set RestContext.response.responseBody. The return value will take precedence and transform the response into valid JSON/XML depending on the Accept header.

2

While the status code is normally automatically set by Salesforce, you can gain control over it through the RestContext's response attribute, which is a HttpResponse object:

// in an unit test, you need to create one
// but in actual code an HttpResponse object already exists
// for the response attribute
HttpResponse httpResponse = new HttpResponse();
RestContext.response = httpResponse;

RestContext.response.setStatusCode(200); // Set the status code to 200 (OK)
RestContext.response.setStatus('All OK!'); // Set the status message to "All OK!"

So yes, you need to write a couple of lines to send back the response code you want with the message you want.

2
  • 2
    Unless you're in a unit test, you don't need to create a new HttpResponse. One is already provided for you in any restresource context.
    – sfdcfox
    Commented Mar 20 at 13:44
  • thanks @sfdcfox, I've added a comment in the sample code regarding unit tests Commented Mar 20 at 18:42

You must log in to answer this question.

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