Nowadays there is an API Integration in almost every org. The main purpose of integration with another service is to avoid reinventing the wheel. However, the development effort that is required to integrate with services is a complex and time-consuming venture. It reduces speed to market, but it also saps developer energy that is better spent in the front end, building the features that will really differentiate their app. With Lightning External Services, Salesforce makes this a lot easier and admin friendly.
With external services, you can connect to any service that you want to, invoke methods based on the external source via a flow all with the help of an easy-to-use wizard. Declarative tools are used to import API definitions right into Salesforce. Swagger or Interagent-based API definitions can be used to define an external service. Once the definitions have been imported, you can create lightning flows that will invoke actions generated from the API definition schema. Below is a depiction of how external services work.
Here is what is happening in the above image:
Based on the provided API schema specification, a schema definition is created that describes the API. Once this is done, named credential is created to authenticate to the serviceâ€™s endpoint using the URL provided by the external service provider. The endpoint is a URL that exposes the web services resources that External Services needs to interact with. Using the named credential and schema definition, external service is registered. External Services imports the definitions into your org and generates Apex actions, which are available immediately in Lightning Flow. While creating a flow, these Apex actions are added into the flow which sends a callout to the endpoint, and output is returned based on schema definition.
Schema Definition for your external service:
Schema specification is basically a contract that contains which type of inputs and outputs can be included in the API calls that are made from your external service. Endpoint information and authentication parameters for REST based API service are also included in specs. On the other hand, schema definition is human readable structured data.
Below is the schema definition of a pet store Swagger API.
This schema declares various methods available in the API and the inputs, outputs included in this service. For example, the below snippet contains information about a GET method which is used to get all the inventories by status.
We will be using the Pet Store Schema to illustrate the whole external service in this blog.
Registering an External Service:
Registering an external service involves the below two steps:
1. Named Credential: In order to register an External service, you need to create a named credential first. Named credential is created to authenticate to the serviceâ€™s endpoint using the URL provided by the external service provider. Create a Named Credential as below in your org:
- For Label, use SwaggerPet.
- For URL, use https://petstore.swagger.io
- Leave other fields as they are and click Save.
2. External Service: In your org, go to setup and search for external service in the quick find box. Create a new external Service and provide the below information in the fields:
- For name, give ExternalSrv1
- For Named credential, select SwaggerPet named credential created in the previous step
- In the Service Schema Relative URL field paste v2/swagger.json
The generated actions are used in the flows. Below is the list of actions available in the pet store endpoint. We cover getOrderById action in this blog.
External Services inflow:
Apex actions generated from external services can be used in lightning flows. When users run the flows, during runtime external services sends a callout to the serviceâ€™s endpoint. Create a new flow and drag the Apex Action element. Select ExternalSrv1_getOrderById__Service
This action takes an Order id (any integer between 1 and 10) and returns details for that order like Order quantity, pet id, shipping date. Create variables for input and output data and configure the output variables to be stored in the variables as done below. Provide the default value of id such that 1<=id<=10. Set the input values and the output values by associating each with a flow variable. Make sure that the data type of input/output matches the input/output specs mentioned in the schema definition.
Connect the Apex Action with the start element.
After completing all these steps, click on debug. Doing this will start the flow in debug mode. On the next screen select Show details of what’s executed and render flow in Lightning runtime and click Run.
Above is the output of ExternalSrv1_getOrderById__Service for order Id = 2. Similarly, other Apex Actions can also be invoked using external services and flow without writing any code.
External services are a great tool which along with point-and-click automation tools like flows and process builders can be used to integrate any API with Salesforce without writing any code. It reduces the development effort and is very much admin friendly making the process of integration making this process simpler and thus cheaper.