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.
Since: 1.0
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
}
}
}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. |
Methods
| Method | Description |
|---|---|
| global API_Inbound() | Constructor. |
| 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. |
Property Details
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.
Since:
Example:
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.
Since:
Example:
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.
Since:
Example:
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.
Since:
Example:
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.
Since:
Example:
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.
Since:
Example:
Method Details
API_Inbound
global API_Inbound()Constructor. Initializes the object and parses the inbound request body.
Since: 1.0
Example:
// Called automatically when the REST endpoint is invoked
private class MyHandler extends API_Inbound { }configure
global override virtual void configure()Initializes class variables and sets up request details including headers and URL parameters.
Since: 1.0
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.
Since: 1.0
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.
Since: 1.0
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:
error(Exception) - The exception that needs to be managed.
Since: 1.0
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.
Since: 1.0
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.
Since: 1.0
Example:
global override void onCommitWorkFinishing()
{
super.onCommitWorkFinishing();
// post-commit logic
}onCommitWorkStarting
global override virtual void onCommitWorkStarting()Performs actions before committing changes to the database.
Since: 1.0
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.
Since: 1.0
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.
Since: 1.0
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.
Since: 1.0
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.
Since: 1.0
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.
Since: 1.0
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.
Since: 1.0
Example:
global override void writeResponse()
{
super.writeResponse();
// customize response writing
}