In this section, we will begin to look at options for making outbound calls to allow us to consume off-platform services from within the Lightning Platform. We will be focusing on services that represent calculations or actions to be performed. For ease, we will be using an existing publicly hosted service running on Salesforce Heroku, which is written in Node.js. Its purpose is to generate a QR code representation of a given input string. The source code for this service has also been included in the /node-service directory in the sample code for this chapter.
External Services empowers the administrator or non-coders to consume external services through other tools such as Lightning Flow and Lightning Process Builder, without writing any code at all. In order to allow this to happen, the developer that authored the service must have exposed a description of the service in a certain format known as Open API (or Swagger). You may recall this since it was covered in Chapter 13, Source Control and Continuous Integration. A portion of the swagger.yaml file in the /node-service/api/swagger folder is shown here:
swagger: "2.0"
# ... see swagger.yaml file for full code ...
paths:
/qrcode:
# binds app logic to a route
x-swagger-router-controller: qrcode
post:
description: Returns QRCode to the caller
# used as the method name of the controller
operationId: qrcode
consumes:
- application/json
parameters:
- in: body
name: body
description: Message to convert to a QR Code
schema:
type: object
required:
- message
properties:
message:
type: string
responses:
"200":
description: Success
schema:
# a pointer to a definition
$ref: "#/definitions/QRCodeResponse"
/schema:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
QRCodeResponse:
required:
- qrcode
properties:
qrcode:
type: string
A full walk-through of the preceding format is outside the scope of this chapter; however, here are a few key aspects to highlight and keep in mind as this definition is consumed by External Services:
- The paths: element defines the URL for each operation exposed by the service; in this case, there is only one, /qrcode. Below that you can see post:. This determines that an HTTP POST method should be used to invoke the operation.
- The x-swagger-router-controller: element indicates the Node.js code file that implements the service being described, which, in this case, is qrcode.js.
- The parameters: and response: elements contain a description of the inputs and outputs supported; in this case, it is a simple string input and output.
Before you can import this definition into the Lightning Platform with External Services, as with the Basecamp service, you must first create a Named Credential, although, in this case, there are no authentication requirements, so an Auth Provider is not required. In the following steps, we will create a simple Named Credential for an existing service:
- Under Setup, search for Named Credentials and click on New.
- Enter QRCode in the Label and Name fields.
- Enter https://createqrcode.herokuapp.com in the URL field.
- Click on Save.
The External Services tool can be found under the Setup menu:
The following steps import the AsciiArt service as an External Service:
- Click on Add an External Service to start the wizard.
- Enter QRCode in the External Service Name field.
- Select QRCode from the Select a Named Credential drop-down field.
- Enter /schema in the Service Schema Relative URL field.
- Click on Next and then Done to complete the wizard.
- From the External Services page, click on the entry you just created:
The preceding screenshot shows the QRCode service configured through an External Service.
Now that the service has been created, it can be referenced from tools such as Lightning Process Builder and Lightning Flow. In the simple Lightning Flow shown in the following screenshot, the Call QRCode Service action was automatically added to the steps available to the user when defining the Flow by completing the preceding External Services wizard. The following Flow simply asks the user for an input message, calls the service, and then shows the resulting output in the last step of the Flow:
As you can see in the following screenshot, the Lightning Flow Designer allows you to map the inputs and outputs of the QR code service (as per the preceding definition) to Flow variables:
Once the Flow is saved, it can be run from the Flow Designer. The following screenshot shows the step requesting the message to be passed to the service:
Finally, the output is displayed in the last step:
In this section, we have used Named Credentials and External Services to import an external service into the platform so that the user can invoke it from tools such as Lightning Flow and Process Builder. If the integration is more of a relational data integration that you wish to combine with existing data in your application objects and/or Salesforce objects, you can leverage External Objects, as described in the following section.