Topics

Use Apex Callouts and Workflow with Heroku

Learning Objectives

After completing this unit, you'll be able to:

Understand how to use Apex callouts and workflow with Heroku.

Understand the use cases for Apex callouts and workflow.

Callouts to Heroku Apps

Sometimes events on Salesforce need to be handled by an external system due to the scale or type of process being executed. For instance, a user in Salesforce uploads an image that needs to be resized for future use. Heroku can receive an event from Salesforce
and perform some process in response. Optionally, the output of the process could be stored back in Salesforce using the REST APIs or Heroku Connect.

There are two primary methods to call a Heroku app based on an event in Salesforce: workflow outbound messages or Apex HTTP callouts. A workflow outbound message declaratively makes a SOAP call. An Apex HTTP callout programmatically makes a REST call
to a Heroku app. Either way, the Heroku app receives a request with the event details payload and then performs the action.

Callouts with Workflow

With workflow, you declaratively define a rule and a callout to an external system. The rule can be connected to any Salesforce object, like Contact or Account, and be triggered based on these record events:

Created

Created, and every time it's edited

Created, and any time it's edited to subsequently meet criteria

Rules must have criteria that filter the events. If you don't want to do any filtering, you can add a criteria that is always true. Here is an example rule:

To call a Heroku app when the rule executes, add an outbound message to the list of immediate workflow actions and specify a Heroku app endpoint as the endpoint URL, like:

If you select Send Session ID, the Heroku app can use that token to make REST API calls on the user’s behalf. If you don't send the session ID. there's no way to check that the request was valid or protect against malicious calls to your
Heroku app’s API endpoint.

On the Heroku side, you can implement the event handler with any open-source web or REST technology. But because the message is in SOAP format, you need to be able to parse the XML. For instance, with JavaScript, Node.js, Express, and the express-xml-bodyparser
library, here is an endpoint that handles an outbound message and parses the SOAP message.

In the request handler, the req.body is the deserialized JSON data sent from the Apex trigger.

With Apex triggers, you can use some form of pre-shared key to authenticate requests, avoiding the potential for malicious requests. You can also have the payload include a session ID to let the Heroku app make REST API requests back to Salesforce to
fetch or update data.