Skip to Main Content

Communicating with External Systems in a Salesforce Application

salesforce ontask

Salesforce applications provide a way to easily extend the capabilities of the popular customer relationship management platform Salesforce. A Salesforce user can choose from thousands of apps that are available on the Salesforce App Exchange

One common capability provided by Salesforce apps is the ability to communicate with external systems in order to extend the functionality of Salesforce. For example, the OnTask Salesforce app provides the ability to launch and monitor OnTask workflows directly in Salesforce.

As a Salesforce app developer, how does one incorporate communication with external systems into an app? Continue reading as we provide a brief introduction to this topic.

Calling REST API Endpoints via HTTP

Web-based services frequently expose their services via a REST API. HTTP is typically the protocol used to access these services.

Salesforce apps are developed using Apex, a strongly typed, object-oriented programming language. Apex allows integration with external REST Web services using callouts. Callouts allow communication with external systems via HTTP. Each callout request is associated with an HTTP method and an endpoint.

 

Communicating with External Systems in a Salesforce Application


In order to develop a callout, several different Apex classes are used. These classes are described below.

HTTP Class

This class is used to initiate an HTTP message exchange and obtain the response.  The method send() is used to send the message.

HTTPRequest Class

This class is used to create an HTTP request that can then be sent using the HTTP class. Various attributes of the HTTP request can be set, such as the method, endpoint, and header fields.

HTTPResponse Class

This class represents the response received from the HTTP class after a request has been sent.  The class has methods for getting various parts of the response, such as getStatusCode() and getBody().

Example

The following example involves calling an OnTask REST API endpoint. This endpoint retrieves the start form associated with a workflow model. The start form allows the user to provide input data when starting a workflow.

In this example, two functions are defined:

  • sendGetWorkflowStartForm() – encapsulates the lowest level processing, which consists of constructing the HTTP request and sending the request
  • getWorkflowStartForm() – calls the lower-level function and processes the response
public static HttpResponse sendGetWorkflowStartForm(String wfModelId) {
	// Create an instance of the Http class
        Http http = new Http();
	// Create an instance of the HttpRequest class
        HttpRequest req = new HttpRequest();
	// Set the HttpRequest method to GET
        req.setMethod('GET');
	// Set the API Endpoint to be called
        req.setEndpoint(baseOnTaskApiUrl + '/workflowModels/' +
wfModelId + '/startEventForm'); 
   // Set the HttpRequest header Content-Type to indicate
   // the returned data should be JSON content
        req.setHeader('Content-Type','application/json');	   // Send the request and store the response in an instance
        // of the HttpResponse class
        HttpResponse res = http.send(req);
        return res;
public static String getWorkflowStartForm(String wfModelId) {
      String baseErrorMsg = 'There was an error getting the start
form. wfModelId: ' + wfModelId;
      try {
	     // Call the function that sends the HTTP Request
          HttpResponse res = sendGetWorkflowStartForm(wfModelId);
	     // Check the status code contained in the HTTP Response
          if (res.getStatusCode() == 404) {
            // A status code of 404 indicates there is no start form.
            // In this case, return null.
            return null;
            else if(res.getStatusCode() == 200) {
            // Return the data contained in the HTTP Response Body.
            return res.getBody();
          } else {
            String errorMsg = 'OnTask returned statusCode: ' +
res.getStatusCode() + ', statusMessage: ' + res.getStatus();
            throw new OnTaskCallerException(errorMsg);
          } catch (Exception e) {
          String errorMsg = e.getMessage();
          throw new OnTaskCallerException(errorMsg);
 

Learn how to send and receive JSON data and how to avoid potential problems you may encounter in the rest of my article here.

Stephen Bucholtz, Principal Engineer

Stephen Bucholtz has been a software engineer at Accusoft since 2005. He has spent most of his time there working on the PICTools SDK. More recently, he has branched out from the world of low-level toolkits to work on the OnTask web application. He is also responsible for the development and maintenance of Accusoft’s highly acclaimed mobile app: Cram. Since receiving his master’s degree in Computer Science from the University of Florida, Stephen has had a lengthy career working on everything from financial systems to embedded software for communications products. When Stephen is away from work, he can often be seen playing drums with various jazz groups around the Tampa Bay area.