API_Inbound
Class · Group: Web Services
global virtual inherited sharing class API_Inbound extends API_BaseExtends: API_Base
Base class for all inbound REST API web service calls. Provides foundational functionality for handling incoming HTTP requests, processing them, parsing request/response DTOs, and sending appropriate responses.
Example
@RestResource(urlMapping='/api/echo/*')
global with sharing class API_Echo
{
@HttpPost
global static void doPost()
{
new EchoHandler().execute();
}
private class EchoHandler extends API_Inbound
{
global override void processRequest()
{
// handle the request
}
}
}Methods
| Method | Description |
|---|---|
| global override virtual void configure() | Initializes class variables and sets up request details including headers and URL parameters. |
| global override virtual String getEncoding() | Retrieves the HTTP character encoding for the request. |
| global override API_Base.HttpMethod getHttpMethod() | Retrieves the HTTP method of the current inbound request. |
| global override virtual void handleError(Exception error) | Handles exceptions during request processing, setting appropriate status codes and writing the response. |
| global override Boolean isDisabled() | Checks custom settings to determine if the API has been disabled for the current user. |
| global override virtual void onCommitWorkFinishing() | Performs actions after a commit is completed, such as setting the response and updating the API call. |
| global override virtual void onCommitWorkStarting() | Performs actions before committing changes to the database. |
| global override virtual void onSuccess() | Registers database changes after a successful call. |
| global virtual void parseRequest() | Parses the inbound request body and populates the requestPayload with deserialized JSON data. |
| global virtual void processRequest() | Handles the processing of the received request. |
| global virtual void updateCallResult() | Updates the results of the call, such as status and response body. |
| global virtual void updateResponseDTO() | Updates the response DTO with relevant information before the response is sent. |
| global virtual void writeResponse() | Writes the response to the client based on the call result. |
configure
global override virtual void configure()Initializes class variables and sets up request details including headers and URL parameters.
Example
global override void configure()
{
super.configure();
requestPayload = new DTO_Request();
responsePayload = new DTO_Response();
}getEncoding
global override virtual String getEncoding()Retrieves the HTTP character encoding for the request.
Returns String — The content type header from the incoming request.
Example
global override String getEncoding()
{
return 'application/xml';
}getHttpMethod
global override API_Base.HttpMethod getHttpMethod()Retrieves the HTTP method of the current inbound request.
Returns API_Base.HttpMethod — The HTTP method of the current request.
Example
HttpMethod method = getHttpMethod(); // Returns the inbound request's HTTP methodhandleError
global override virtual void handleError(Exception error)Handles exceptions during request processing, setting appropriate status codes and writing the response.
Parameters
| Parameter | Type | Description |
|---|---|---|
error | Exception | The exception that needs to be managed. |
Example
global override void handleError(Exception error)
{
super.handleError(error);
// custom error handling
}isDisabled
global override Boolean isDisabled()Checks custom settings to determine if the API has been disabled for the current user.
Returns Boolean — True if the API is disabled, false otherwise.
Example
global override Boolean isDisabled()
{
return super.isDisabled() || customDisableCheck();
}onCommitWorkFinishing
global override virtual void onCommitWorkFinishing()Performs actions after a commit is completed, such as setting the response and updating the API call.
Example
global override void onCommitWorkFinishing()
{
super.onCommitWorkFinishing();
// post-commit logic
}onCommitWorkStarting
global override virtual void onCommitWorkStarting()Performs actions before committing changes to the database.
Example
global override void onCommitWorkStarting()
{
super.onCommitWorkStarting();
// pre-commit logic
}onSuccess
global override virtual void onSuccess()Registers database changes after a successful call. Simulates an error for testing if returnErrorResponse is set.
Example
global override void onSuccess()
{
super.onSuccess();
doInsert(newRecord);
}parseRequest
global virtual void parseRequest()Parses the inbound request body and populates the requestPayload with deserialized JSON data.
Example
global override void parseRequest()
{
super.parseRequest();
// additional request parsing
}processRequest
global virtual void processRequest()Handles the processing of the received request. Implement in descendant classes to define specific logic for handling API endpoints.
Example
global override void processRequest()
{
DTO_Request dto = (DTO_Request)requestPayload;
// process the inbound request
}updateCallResult
global virtual void updateCallResult()Updates the results of the call, such as status and response body. Override in descendant classes to implement specific update logic.
Example
global override void updateCallResult()
{
super.updateCallResult();
// customize the response body
}updateResponseDTO
global virtual void updateResponseDTO()Updates the response DTO with relevant information before the response is sent. Override in inheriting classes to customize the response body data.
Example
global override void updateResponseDTO()
{
DTO_Response dto = (DTO_Response)responsePayload;
dto.recordId = newRecord.Id;
}writeResponse
global virtual void writeResponse()Writes the response to the client based on the call result. Override to modify the response written to the client.
Example
global override void writeResponse()
{
super.writeResponse();
// customize response writing
}Constructors
| Constructor | Description |
|---|---|
| global API_Inbound() | Constructor. |
API_Inbound()
global API_Inbound()Constructor. Initializes the object and parses the inbound request body.
Example
// Called automatically when the REST endpoint is invoked
private class MyHandler extends API_Inbound { }Properties
| Property | Description |
|---|---|
| global RestRequest request | Reference to the current inbound REST request. |
| global String requestBody | Returns the request body from the current inbound HTTP request. |
| global DTO_JsonBase requestPayload | The DTO that will be populated from the inbound request body. |
| global RestResponse response | Reference to the current REST response object. |
| global String responseBody | Manages the response body that will be sent to the client. |
| global DTO_JsonBase responsePayload | The DTO that will be serialized to populate the response body. |
request
global RestRequest requestType: RestRequest
Reference to the current inbound REST request. Provides access to the details of the incoming request, such as headers, parameters, and the request body.
See Also: RestRequest
requestBody
global String requestBodyType: String
Returns the request body from the current inbound HTTP request. If the request body is null or empty, it returns an empty string.
requestPayload
global DTO_JsonBase requestPayloadType: DTO_JsonBase
The DTO that will be populated from the inbound request body. Handles the request data in JSON format and ensures proper initialization and deserialization.
response
global RestResponse responseType: RestResponse
Reference to the current REST response object. Allows manipulation of the response that will be sent back to the client, including setting the response body and status code.
See Also: RestResponse
responseBody
global String responseBodyType: String
Manages the response body that will be sent to the client. Ensures that the value is stored as a Blob in the response object.
responsePayload
global DTO_JsonBase responsePayloadType: DTO_JsonBase
The DTO that will be serialized to populate the response body. Handles the response data in JSON format and ensures proper initialization and serialization.