Courseiva

SF-PD2 Advanced Developer Fundamentals Practice Question

A developer is building a custom REST API using Apex. The API must return a response with a specific HTTP status code and a JSON body that includes custom error details. Which approach should be used to construct the response?

⚠ Common exam trap

The trap here is assuming that returning a Map or throwing an exception can control the HTTP status code, when only RestContext.response provides that level of control.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Use RestContext.response to set the status code and body

To return a custom HTTP status code and a JSON body from an Apex REST method, you must use RestContext.response. This object provides access to the response and allows setting the statusCode and responseBody properties. Returning a Map or throwing an exception does not give control over the status code or the ability to include structured error details.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Use ApexPages.currentPage().getParameters() to set response details

    Why it's wrong here

    ApexPages.currentPage() is used in Visualforce controllers and has no relevance to REST API responses. It cannot set HTTP status codes or response bodies for a custom REST endpoint. Using it in a REST context would result in a runtime error or have no effect.

  • ✗

    Throw an ApexException with a custom message

    Why it's wrong here

    Throwing an exception in Apex REST results in a generic 500 error response with a standard error message. It does not allow setting a custom HTTP status code or including a structured JSON body with error details. This approach lacks the flexibility needed for a well-defined API error contract.

  • ✗

    Return a Map<String, Object> from the method

    Why it's wrong here

    Returning a Map from an Apex REST method automatically serializes it to JSON, but it does not allow you to set the HTTP status code. The default status code is 200 OK, which is not suitable for error responses. You cannot include custom error details in the status line or headers with this method.

  • ✓

    Use RestContext.response to set the status code and body

    Why this is correct

    RestContext.response allows direct manipulation of the HTTP response in a custom REST Apex class. You can set the statusCode property and assign a response body as a Blob or String. This is the standard way to return custom status codes and structured JSON error details from an Apex REST method.

About these practice questions

Courseiva writes every SF-PD2 question from scratch — 226 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Salesforce exam blueprint

This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.