Use External Services in a Flow

Learning Objectives

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

  • Find your External Services actions in the Flow Builder toolbox.
  • Create a simple flow with your External Services actions.

Before You Start

  1. Before you can complete this module, we recommend that you first complete the Build a Simple Flow module and secondly, the Flow Builder module. This unit depends on the concepts you learn in the two Flow modules. A little extra preparation will yield great Salesforce no-code superpowers when you complete this module!
  2. Complete the previous unit, where you created a Named Credential and registered a "BankService" external service using the External Services wizard. This unit depends on both the Named Credential and the new external service you created.

Build a Flow To Access External Business Actions

Let’s build a flow that puts into practice what you've learned and set up so far. Ready to put your skills to the test and implement our business process? Here we go! 
Note
The Named Credential and External Services registration in the previous unit make our external web actions available from Flow.

Project: To deliver a more personalized experience, our fictitious bank wants to use Salesforce to track sales activity on bank accounts. The bank's new business process is going to enable bank representatives to add bank accounts from within Salesforce, while ensuring that bank account details like bank account type and bank account ID are synchronized between the Salesforce User record and the bank's backend service. Since more than one rep will be adding accounts, our business logic needs to be able to check for an existing bank account, or to create a new bank account.
To automate this process, we’re going to register the bank web service as an External Service. Once our bank service is registered, the bank service’s account API operations (add, update, retrieve) are visible and accessible for use within Salesforce with the Flow Builder tool. At this point, we have everything we need! We'll create a flow that automatically adds a new bank account or updates an existing bank account's details - and sends any new information from the bank service to Salesforce each time a bank representative accesses the account. 

You’ll be challenged to complete some of these business actions and enhance them later, but here’s a sneak peek at the new business process we’re going to build in Flow Builder. 
Let’s take a closer look at the stages and logic so you can see how the information flows. Note the five "steps" that we'll make to complete this flow.

  1. Screen Bank Account Record: Captures account name and account type from information that the bank representative enters on the screen.
  2. Action Add Bank Account: Having registered the bank service's API specification with External Services, all API operations defined in our spec are now available for use within the flow as matching actions. We use the addAccount action and specify the API's inputs and outputs which we'll assign to variables to be used between the Salesforce org and the bank service.
  3. Decision Bank Account Updated?  Logic evaluates if a bank account exists and needs updating, or creates a new bank account, if needed.
  4. Assignment Assign Existing Bank Account ID: If a bank account exists, assigns the account ID of the existing bank account and uses this value to update the Salesforce User account.
  5. Update Records Update Salesforce User Record: If the Salesforce user's last name matches a bank account record, then our flow updates the Salesforce user record with the bank account type and the bank account ID. If no Last Name match is found at the bank service, and a new bank account record is created, then it updates the Salesforce user record (if there's a matching name in Salesforce) with the new bank account type and bank account ID.

And to prepare our Salesforce Org we need to: 

  • Create two new custom fields on the User object: Account Type and Account ID, so that each time we enter a new bank customer, we have fields to store the bank account type and bank account ID in the Salesforce User record.
  • And (ding!) the bank has a new customer, so you, our trusty bank representative, will first create a new Salesforce User.

We'll walk through these steps in the next section.

External Services Actions in Flow

If you completed the previous unit, you’ve registered an external service using the External Services wizard and viewed the External Services actions in the wizard. To see the actions, let’s jump into Flow Builder.

  1. From Setup, enter Flow in the Quick Find box, then select Flows.
  2. Click New Flow, select Screen Flow, click Next, and then select Freeform.
  3. From the toolbox, with the Elements tab selected, drag Action onto the canvas.
  4. To see the actions imported by External Services based on the API spec we used in the previous unit, change Filter By to Type, select External Service, and click in the search box.
  5. To see the actions grouped by an External Services registration (in this example, BankService), change Filter By to Category, select BankService, and click in the search box. When you’re done viewing the available BankService actions, we can move on.
  6. Click Cancel and return to Setup.

List of added actions from External Service registration

For example, you can see the getAccount action that we looked at in the External Services wizard, along with addAccount, DeleteAccount, and updateAccount. These External Services actions are the API operations pertaining to our fictional bank that we can use in a flow to get information, add account records, and so forth. External services that you create can have many more actions; it depends on the specific service that you use and how the API spec's schema is defined for your use case.

We’re about to use these actions in a flow of our own. 

First, in Salesforce, let’s create two custom fields, Account Type and Account ID, on the User object, so that we have fields in the user's record to store the bank account type and bank account ID values that we retrieve from the bank service.

  1. From Setup, select Object Manager, then select User.
  2. Click Fields & Relationships, then click New (Custom Field).
  3. Under Data Type, select Text and click Next.
  4. For Field Label, use Account Type and for Length use 25. Leave the rest of the defaults, and click Next.
  5. Make sure that the field-level security is set to visible for profiles you use in your org and click Next, then Save & New.
  6. Under Data Type, select Text and click Next.
  7. For Field Label, use Account ID and for Length use 25. Leave the rest of the defaults, and click Next.
  8. Make sure that the field-level security is set to visible for profiles you use in your org and click Next, then Save.

In Salesforce, let’s create a user record for our client, Maria Thompson.

  1. From Setup, enter Users in the Quick Find box, then select Users.
  2. Click New User.
  3. For First Name, enter Maria
  4. For Last Name, enter Thompson
  5. For User License, select Salesforce Platform.
  6. For Profile, select Standard Platform User.
  7. Since this is a test org, use your own email address.
  8. Enter a username and nickname to use (or just use the auto-filled values) and Save.
If you're curious, you can now verify that our user, Maria Thompson, has the two new custom fields in her Salesforce user account (Account Type and Account ID). As you might expect, both fields are currently empty.

Maria Thomson would like to open a checking account at our bank. We've created a user for her in Salesforce (a necessary step, based on our business logic) - now, let's make it really easy for bank representatives to not only complete the new account creation process, but leverage all of the power of Salesforce to track new bank accounts.


Let’s get started on our flow!

Create a Flow Using External Services Actions

Ready to get to it? Let’s begin by building a flow that can create new bank accounts, or update existing bank accounts at our fictional bank. 

Screen
  1. From Setup, enter Flows in the Quick Find box, then select Flows.
  2. Click New Flow, select Screen Flow, and click Next.
  3. Select Freeform.
  4. From the toolbox, with the Elements tab selected, drag the Screen element onto the canvas.
    • Label: Bank Account Record
    • API Name: Bank_Account_Record
  5. From the list of Input Components, drag two Text components into the Flow Label screen canvas.
  6. Click to highlight the first Text component
    • Label: Last Name on Account
    • API Name: Last_Name_on_Account
  7. Click the second Text component
    • Label: Bank Account Type
    • API Name: Bank_Account_Type
  8. Click Done.
  9. Click Save to save the flow and set the flow’s properties.
    • Flow Label: Bank Account Records
    • Flow API Name: Bank_Account_Records
  10. Click Save and ignore any warnings.
Note
So you don’t lose your work, save early and often while building your flow.
So far, we simply added a screen element to our flow that enables two text inputs. Now it's time for action

"Action" elements, by definition, present and perform invocable actions outside of the flow that were created by External Services when you registered your API spec. Calling one of the actions registered by your external bank service is an example of invoking an action outside of the flow.

We’re interested in the ability to add an account at our fictional bank from within Salesforce. "Wait... don't I have to be a really nerdy developer to do that?" Not at all! External Services has done all the nerdy heavy-lifting for you! And now, you simply use a tool like Flow Builder or Einstein Bots to hook up the connections - to choreograph your perfect data ballet.

To continue, let’s add the addAccount action, originally an operation from our API spec, as an action element in our flow; such that - if an existing bank account doesn't already exist - we use the addAccount action to create a new bank account at the bank service. This action takes the two user inputs, calls the bank service via our BankService Named Credential, and looks for an existing bank account with both a matching name and account type.

There are two output values from the addAccount operation in our API spec that we will define here as variables. 

  • 201:  a name match was not found. There is presently no bank account. Therefore, a new bank account is created via addAccount.
  • 409:  an exception that means a name match was found - the bank account already exists - so we don’t create a new one. In this case, a message is returned within the flow along with existing account details. The details are returned so that we know exactly which account triggered the 409 exception.

We'll define these output values here to make other downstream elements in our flow smarter.  Ok. Let's be clear: italics won't give you Salesforce super-powers... but completing the rest of this unit and the hands-on-challenge... will.

Action

  1. From the toolbox, with the Elements tab selected, drag the Action element onto the canvas.
  2. Change Filter By to Type, select External Service, and click in the search box.
  3. Select addAccount.
    • Label: Add Bank Account
    • API Name: Add_Bank_Account
  4. Under Set Input Values, click in the accountName field, enter Name and select Last_Name_on_Account.
  5. Under Set Input Values, click in the accountType field, enter Type and select Bank_Account_Type.
  6. Expand the Advanced section, and check Manually assign variables.
  7. Under Store Output Values, click in the _201 field, select New Resource.
  8. Under New Resource
    • Resource Type: Variable
    • API Name: account
    • Data Type: Apex-Defined
    • Apex Class: ExternalService__BankService_accountDetails
    • Availability Outside the Flow: Available for input and Available for output
  9. Click Done.
  10. Under Store Output Values, click in the _409 Exception field, select New Resource.
    • Resource Type: Variable
    • API Name: existingAccount
    • Data Type: Apex-Defined
    • Apex Class: ExternalService__BankService_accountDetails
    • Availability Outside the Flow: Available for input and Available for output
  11. Click Done.



With so many bank representatives signing up new customers, who knows for sure whether an account for Maria Thompson already exists? So, let's add logic for evaluating whether the account already exists or not.  

When the flow executes, if the account doesn’t exist, then our flow is routed through a "create account" path and proceeds to update the new user account via addAccount. If the account already exists (default outcome), it is routed through an "update" path, and we simply update the Salesforce user record with the information from the bank service. This type of evaluation and branching logic is perfect for Flow Builder's Decision element! 

Decision
  1. From the Elements tab in the toolbox, drag Decision onto the canvas.
  2. At the top of the New Decision window, enter these values.
    • Label: Bank Account Updated?
    • API Name: Bank_Account_Updated (Remove the "?" at the end of the string for the API_Name)
  3. In the Outcomes section, notice the two listed outcomes on the left: New Outcome and Default Outcome.
  4. Make sure that New Outcome is selected, and enter these values into the Outcome Details section.
    • Label: Bank Account Created
    • API Name: Bank_Account_Created
    • For Condition Requirements to Execute Outcome, select All Conditions Are Met (AND).
    • For Resource, enter these conditions.
      Resource Operator Value
      existingAccount Is Null $GlobalConstant.True
    • Notice that the existingAccount resource does not specify a suffix such as existingAccount.id or existingAccount.name - that's because we want to check for a match across the whole object - not just a single field. This means that the name and account type, given at the initial screen input, must both match in order to update an existing account. If we don't find a match, we create a new account. 
  5. Select Default Outcome and enter:
    • Label: Bank Account Found
  6. Click Done.
Note
Notice that "Bank Account Found" has no conditions. That’s because it’s the default outcome. If the conditions aren’t met for the flow to create an account, the flow always takes this path and updates the account information.

In the case where a bank account already exists, let’s tell Flow to update the Salesforce User Account ID field with the Bank account ID. To do this, we need to assign the value to a variable (account.id).

Assignment
  1. From the toolbox, drag Assignment onto the canvas.
  2. In the New Assignment window, enter these values.
    • Label: Assign Existing Bank Account ID
    • API Name: Assign_Existing_Bank_Account_ID
  3. For Set Variable Values, enter these values
    Variable Operator Value
    account.id Equals existingAccount.id
  4. Click Done and ignore any warnings
Note
You can’t copy and paste the Variable values. For the variable, type account, and click on account under Apex-Defined Variable and then select id.

Let's use the values stored in variables to update our Salesforce user record with the bank account type and the bank account ID

Update Records

  1. From the toolbox, drag the Update Records element onto the canvas.
  2. In the New Update Records window, enter these values:
    • Label: Update Salesforce User Record
    • API name: Update_Salesforce_User_Record
  3. Select Specify conditions to identify records, and set fields individually
  4. Under Update Records of This Object Type
    • Object: User
  5. Under Filter User Records
    • Condition Requirements for record to update: All Conditions are Met (AND)
    • Field: LastName
    • Operator: Equals
    • Value: Last_Name_on_Account
  6. Under Set Field Values for the User Records
    • Field: Account_ID__c
    • Value: account.id (Under Apex-Defined)
  7. Select + Add Field
    • Field: Account_Type__c
    • Value: Bank_Account_Type (under Screen Components, not under Apex-Defined)
  8. Click Done.

We also need two record variables for the bank account owner's Salesforce User record.

Create Two Record (Single) Variables

  1. From the Toolbox, select the Manager tab.
  2. Click New Resource and create a variable with these properties.
    • Resource Type: Variable
    • API Name: accountTypeField
    • Data Type: Record
    • Object: User
    • Availability Outside the Flow: Available for output
  3. Click Done.
  4. From the Toolbox, select the Manager tab.
  5. Click New Resource and create a variable with these properties.
    • Resource Type: Variable
    • API Name: user
    • Data Type: Record
    • Object: User
    • Availability Outside the Flow: Available for output
  6. Click Done.

Almost there! We just need to hook up our flow. Let’s start connecting the elements in the right order by dragging the connector node to the target element so our business process flows successfully at run time. Don’t forget to save it!  

  1. Connect Start to Screen Bank Account Record.
  2. Connect the Screen Bank Account Record to Action Add Bank Account.
  3. Connect Action Add Bank Account to Decision Bank Account Updated?
  4. Align the Decision element on the canvas so you have space for two outgoing connectors on either side.
  5. Connect Decision Bank Account Updated? outcome to Assignment Assign Existing Bank Account ID.
  6. When prompted, select Bank Account Found for the decision outcome to assign to the path and click Done.
  7. Connect Assignment Assign Existing Bank Account ID to Update Records Update Salesforce User Record.
  8. Connect Decision Bank Account Updated? to Update Records Update Salesforce User Record.
  9. Click Save.

Almost done! 

Let’s use debug mode to check your flow. 

Click Debug, select Show details of what's executed and render flow in Lightning runtime, and click Run

For Last Name on Account, type Thompson and for Bank Account Type, enter Checking

Click Next.

The flow executes with our inputs. Under Debug Details, follow the entire flow that you just built. First, there's your text inputs. Next, under BANKSERVICE.ADDACCOUNT, we can see that the text inputs have been properly assigned, and that the the outputs report
either an existing account:


{!existingAccount} = 409exc

and

{!account} = 201 (null)

or that no account was found, and a new one was created:

{!existingAccount} = 409exc (null)

and

{!account} = 201

The Decision phase confirms the outcome of the decision. The variables were assigned.


And now, under Update Records, the Account Type and Account ID fields in Salesforce have been populated with the new values from the bank web service!  

Note

Don't worry if you encounter different results (201 or 401). The result varies according to the application's refresh of accounts.

(drum roll, please) Now - look at Maria Thompson's User record in Salesforce.

As the bank representative, we entered the name and account type in Salesforce and our Flow app automatically updated an existing bank account (or created a new one) at the bank service and returned the account ID and the new account type to the Salesforce User record. You now have a way to track sales activity on bank accounts all from within Salesforce! "No code", did you say? Yes. All done without writing a single line of code.

Limits of our simple app...

What you also need to know is that you could have received an error. For example, if you used "Maria Thompson" for Last Name on Account, you would have received an error, because a space is illegal in this field. Likewise, an error might be displayed if you enter a name that is less than six characters, ("Jones"), since this is another limit of our simple application. Here are some of the codes you might see.

Code Description for Acme Bank Service
200 System finds an account with a given name
201 Response when the account does not exist and we can create one
204 Response when system finds an account with given name
400 Error response if the account name is less than minimum of six characters or has spaces
404 Error response if the account is not supported by service or not found
409 Response when the account exists and we cannot create one

Note

If we had created a flow to use with an actual external banking service, we would create records using unique identifiers and not names! There is other logic that is surely too elementary for a banking scenario, but again, we’re keeping it simple here so that you can focus on understanding External Services. 

The Recap

Remember that diagram from the first unit? Here it is again to show you the path and the steps we took along the way. Notice that while we had to deal with an OpenAPI-compliant schema definition within our API spec, most of the work is declarative (low to no coding). Nice, huh?

6 Steps of the External Services workflow

Extend your External Service app and get hands-on

Major stakeholders at the bank really like the bank service project you just completed in the previous section. Now they want to add another business process to the existing bank service workflow. 

Here's what the additional four steps will look like once you're complete. 


Note

If you haven’t already done so, complete the previous bank external service project in the Build a Flow To Access External Business Actions section before attempting this extension. Otherwise, we won’t have anything to verify.

Let's add an action (step 6 in the image) to the flow you've already created in this unit. If the account already exists, then show the account information to the bank representative. If the account doesn’t exist, we’re going to tell the bank representative. Steps 7-9 are covered in the hands-on challenge.

Let’s start by creating two variables to store the information about whether a bank account already exists and can be confirmed, and another variable to cover the case where a bank account doesn’t exist. 

Create Two Apex-Defined Variables

  1. Return to your Bank Account Records flow.
  2. From the toolbox, with the Manager tab selected, create a new resource.
    • Resource Type:  Variable
    • API Name: checkedAccount
    • Data Type: Apex-Defined
    • Apex Class: ExternalService__BankService_accountDetails
    • Availability Outside the Flow: Check both Available for input and Available for output
  3. Click Done
  4. Create a new resource for the second variable.
    • Resource Type: Variable
    • API Name: notFoundAccount
    • Data Type: Apex-Defined
    • Apex Class: ExternalService__BankService_errorModel (make sure you select the one with "errorModel" at the end)
    • Availability Outside the Flow: Check both Available for input and Available for output
  5. Click Done
Action
  1. From the toolbox, with the Elements tab selected, drag the Action element onto the canvas.
  2. Change Filter By to Category, select BankService and click in the search box.
  3. Select getAccount
    • Label: Verify Bank Account
    • API Name: Verify_Bank_Account
    • Set accountName: Last_Name_on_Account
    • With the Advanced section expanded, check Manually assign variables
  4. Under Store Output Values:
    • >_200: checkedAccount (Apex-Defined Variable)
    • >_404 Exception: notFoundAccount (Apex-Defined Variable)
  5. Click Done
  6. Connect the Update Records Update Salesforce User Record element to the Action Verify Bank Account element.
  7. Click Save

Resources

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities