How to use External Services in Salesforce: A Step-by-Step Guide

How to use External Services in Salesforce A Step-by-Step Guide Salesforce Shastras

Salesforce External Services is a powerful feature that allows you to seamlessly connect with external REST APIs and services directly from your Salesforce org. Whether you’re a developer, administrator, or business user, understanding how to leverage External Services can enhance your Salesforce processes and streamline integrations. In this guide, we’ll walk through the steps to set up and How to use External Services in Salesforce effectively.

What is External Services in Salesforce?

External Services enable you to integrate with external systems, such as RESTful APIs. By registering an external service in Salesforce, you can create reusable actions that can be invoked declaratively within flows, process builder, Apex code, Omni Script or Bot. These actions allow you to interact with external services without writing custom code.

Use declarative tools and OpenAPI specifications to describe the external API functionality, and External Services automatically creates invocable actions within Salesforce.

With External Services, you first register OpenAPI 2.0 or OpenAPI 3.0 schemas. The operations imported from your registered schema automatically become invocable in Apex, or as External Services action types within point-and-click automation tools such as Flow Builder, Orchestrator, Einstein bots, or OmniStudio Assets.

External Services is best used when the externally hosted service is a RESTful service and the API specification is available in OpenAPI 2.0 or OpenAPI 3.0 JSON or YAML schema format.


A Step-by-Step Guide

Step 1: Create a Named Credentials

To use an external service, we need to create Named Credentials first.

There are two types of Named Credentials available in Salesforce:

  • Legacy Named Credentials
  • Enhanced Named Credentials

We are going to use Legacy Named Credentials

Named Credentials Info

Label: Employee API

URL: https://th-external-services.herokuapp.com

Note: Leave other fields as it is.

How to use External Services in Salesforce

Once we created named credentials, we will create an external service.

Click on SETUP -> EXTERNAL SERVICES

How to use External Services in Salesforce

On the External Service Interface, you will get two options to connect with the external service API.

From API Specification & MuleSoft Services

We will select From API Specification

How to use External Services in Salesforce

External Service Name: bankService44

Description: Account Sync

Service Schema: /accounts/schema

URL: Relative URL

Select a Named Credential: Employee_API

Once we enter all the above information, the external service will fetch all the information about the API. and display it in swagger JSON format, where you will get all the information about the API and parameter details.

{
    "swagger": "2.0",
    "basePath": "/",
    "info": {
        "version": "1.0",
        "title": "External Service for demo bank",
        "description": "### External Service for demo bank",
        "x-vcap-service-name": "DemoBankRestServices"
    },
    "securityDefinitions": {
        "basicAuth": {
            "type": "basic"
        }
    },
    "security": [
        {
            "basicAuth": [
            ]
        }
    ],
    "tags": [
        {
            "name": "DemoBankRestServices"
        }
    ],
    "paths": {
        "/accounts/{accountName}": {
            "get": {
                "operationId": "getAccount",
                "summary": "Retrieves an account",
                "description": "Retrieves the account with specific name",
                "consumes": [
                    "text/plain"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "accountName",
                        "in": "path",
                        "required": true,
                        "type": "string",
                        "description": "Name of the account"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "The response when system finds an account with given name",
                        "schema": {
                            "$ref": "#/definitions/accountDetails"
                        }
                    },
                    "400": {
                        "description": "Error response if the account name parameter is less than minimum characters",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    },
                    "404": {
                        "description": "Error response if the account is not supported by service or account is not found",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    }
                }
            },
            "delete": {
                "operationId": "DeleteAccount",
                "summary": "Deletes an account",
                "description": "Deletes the account with specific name",
                "consumes": [
                    "text/plain"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "accountName",
                        "in": "path",
                        "required": true,
                        "type": "string",
                        "description": "Name of the account"
                    }
                ],
                "responses": {
                    "204": {
                        "description": "The response when system finds an account with given name",
                        "schema": {
                            "type": "string"
                        }
                    },
                    "400": {
                        "description": "Error response if the account name parameter is less than minimum characters",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    },
                    "404": {
                        "description": "Error response if the account is not supported by service or account is not found",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    }
                }
            },
            "post": {
                "operationId": "addAccount",
                "summary": "Add an account",
                "description": "Add an account to the database",
                "consumes": [
                    "text/plain"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "accountName",
                        "in": "path",
                        "required": true,
                        "type": "string",
                        "description": "Name of the account"
                    },
                    {
                        "name": "accountType",
                        "in": "query",
                        "required": true,
                        "type": "string",
                        "description": "The type of account"
                    }
                ],
                "responses": {
                    "201": {
                        "description": "The response when the account does not already exist and we can create one",
                        "schema": {
                            "$ref": "#/definitions/accountDetails"
                        }
                    },
                    "409": {
                        "description": "The response when the account already exists and we cannot create one",
                        "schema": {
                            "$ref": "#/definitions/accountDetails"
                        }
                    },
                    "400": {
                        "description": "Error response if the account name parameter is less than minimum characters",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    },
                    "404": {
                        "description": "Error response if the account is not supported by service or account is not found",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    }
                }
            },
            "put": {
                "operationId": "updateAccount",
                "summary": "Updates an account",
                "description": "Updates the account with specified name",
                "consumes": [
                    "text/plain"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "accountName",
                        "in": "path",
                        "required": true,
                        "type": "string",
                        "description": "Name of the account"
                    },
                    {
                        "name": "accountType",
                        "in": "query",
                        "required": true,
                        "type": "string",
                        "description": "The type of account"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "The response when system finds an account with given name",
                        "schema": {
                            "$ref": "#/definitions/accountDetails"
                        }
                    },
                    "400": {
                        "description": "Error response if the account name parameter is less than minimum characters",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    },
                    "404": {
                        "description": "Error response if the account is not supported by service or account is not found",
                        "schema": {
                            "$ref": "#/definitions/errorModel"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "accountDetails": {
            "required": [
                "id",
                "name",
                "type",
                "availableBal"
            ],
            "properties": {
                "id": {
                    "type": "string",
                    "description": "id"
                },
                "name": {
                    "type": "string",
                    "description": "name"
                },
                "type": {
                    "type": "string",
                    "description": "type"
                },
                "availableBal": {
                    "type": "string",
                    "description": "availableBal"
                }
            }
        },
        "errorModel": {
            "required": [
                "errorCode",
                "errorMessage"
            ],
            "properties": {
                "errorCode": {
                    "type": "string",
                    "description": "A service-specific error code."
                },
                "errorMessage": {
                    "type": "string",
                    "description": "A service-specific error code."
                }
            }
        }
    }
}
How to use External Services in Salesforce

Once the we review Swagger, click on next and you will see below option, where you can see the operations available for the API with Input, Output Parameters.

we can select all of any which you are going to use.

How to use External Services in Salesforce

On clicking next, you will see some apex classes have been created that can be used in flow, apex calls, bots, and omni scripts.

201

The response when the account does not already exist and we can create one

Type: Object

Apex Class: bankService44_accountDetails

After clicking on Finish, you will see that an external service has been created with more information, such as Active Operations, Total Operations, Active Objects, Total Objects, and Credentials. You can also see available external services and objects.

We can review, edit, delete or save As these external services

View Action will show available methods


Use case

Create a screen flow where User will enter “last name” and “Account Type”, and External API will show user’s current balance on screen.




After calling external API, we get the below response and from this response we will display balance on flow screen

(bankService44_accountDetails : { "z0type_set" : true, "z0type" : "Checking", "name_set" : true, "name" : "Thompson", "id_set" : true, "id" : "131", "availableBal_set" : true, "availableBal" : "$0" })
responseCode (200)

Here are some practical examples of how External Services can be used

  1. Credit Scoring Service Integration: You can integrate a credit scoring service into account pages to display credit limits for your Salesforce accounts.
  2. Eligibility Verification: Use an external verification service to check eligibility for discounts. This ensures that only eligible customers receive applicable discounts.
  3. Digital Payment Services: Incorporate flexible digital payment services for processing orders. This allows for smooth and secure payment transactions.
  4. Mapping Services with Visualization: Bring in mapping services that provide visualization tools. These can be useful for territory planning and optimizing field operations.

Considerations and limitations:

  1. OpenAPI Support: External Services support both OpenAPI 2.0 and 3.0 specifications. However, ensure that your external service adheres to these standards for successful integration.
  2. Inbound Callouts: External Services do not support inbound callouts for webhooks and templates. This means you can’t create custom integrations that rely on two-way communication between systems.
  3. Charset and Class Names: Be cautious with dynamically created Salesforce classes. There might be issues related to character sets and class names, although recent releases have improved this aspect.
  4. Callout Limits: In a single Apex transaction, you can make a maximum of 100 callouts to external services. However, in Developer Edition orgs, you’re limited to 20 concurrent callouts to endpoints outside your Salesforce org’s domain.

Best Practices:

  • Error Handling: Handle errors gracefully in your flows. Consider using fault connectors to handle exceptions from external services.
  • Security: Ensure that the external service endpoint is secure and properly authenticated.
  • Governor Limits: Be aware of Salesforce governor limits when invoking external services.

Remember, External Services empower you to build seamless integrations without writing extensive code. Explore the possibilities and enhance your Salesforce org’s capabilities!

Leave a Reply

Your email address will not be published. Required fields are marked *