6

We have exposed an Apex Class as a Rest Resource. It is a HTTP Post class and broadly it has the following logic:

  1. Get parameters to create a new Account
  2. Validate if existing database if it is a duplicate
  3. If yes, throw error.
  4. If no, create Account and return the Salesforce ID of the Account.

The response is returned in a JSON format. What I want is to also be able to set the HTTP Response Status parameter. It is by default set by Salesforce as 200 but I want to set it based on various conditions in my logic (For e.g.: 400 in step 3 on above logic).

Is this possible?

10
RestContext.response.statusCode = 400;

You can set any status code defined in RestResponse; simply set RestContext.response.statusCode to any value you'd like from the following list:

200 OK
201 CREATED
202 ACCEPTED
204 NO_CONTENT
206 PARTIAL_CONTENT
300 MULTIPLE_CHOICES
301 MOVED_PERMANENTLY
302 FOUND
304 NOT_MODIFIED
400 BAD_REQUEST
401 UNAUTHORIZED
403 FORBIDDEN
404 NOT_FOUND
405 METHOD_NOT_ALLOWED
406 NOT_ACCEPTABLE
409 CONFLICT
410 GONE
412 PRECONDITION_FAILED
413 REQUEST_ENTITY_TOO_LARGE
414 REQUEST_URI_TOO_LARGE
415 UNSUPPORTED_MEDIA_TYPE
417 EXPECTATION_FAILED
500 INTERNAL_SERVER_ERROR
503 SERVER_UNAVAILABLE
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.