Accessing external services via External Services

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.

This chapter does not cover how to deploy the service code to Heroku, since there is already a publicly running version of it that can be referenced for you to use. However, if the sample service is down, or if you just prefer to know more about deploying Heroku Node.js applications, then copy the contents of the /node-service folder to a new folder and follow the steps here: https://www.heroku.com/nodejs.

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 13Source 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.
When using the External Services tool, you can either paste a definition given to you by a developer or point the tool to a URL where the definition already exists. It is best practice to provide the definition with the service so that the two are maintained together. The preceding definition is co-located with the external service on the internet here: https://createqrcode.herokuapp.com/schema/.

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:

  1. Under Setup, search for Named Credentials and click on New.
  2. Enter QRCode in the Label and Name fields.
  3. Enter https://createqrcode.herokuapp.com in the URL field.
  4. 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:

  1. Click on Add an External Service to start the wizard.
  2. Enter QRCode in the External Service Name field.
  3. Select QRCode from the Select a Named Credential drop-down field.
  4. Enter /schema in the Service Schema Relative URL field.
  5. Click on Next and then Done to complete the wizard.
  6. From the External Services page, click on the entry you just created:

The preceding screenshot shows the QRCode service configured through an External Service.

At the time of publishing, there is an Enhanced External Services Pilot that adds the ability to support more complex inputs and outputs than just the basic data types used in the preceding example.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.134.29