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.

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

Note

The Named Credential and External Services registration in the previous unit make our external web actions available for reference from flow for our Salesforce org.

Project: To deliver a more personalized experience, Acme bank wants to use Salesforce to track sales activity on accounts. Acme’s new business process is going to enable an Acme bank representative to add an account record in Salesforce, while ensuring account details like account type and account are synchronized with the bank's backend service account. The business process also synchronizes updates to customer information (user) in Salesforce. 
To automate this process, we’re going to register the backend bank service as an External Service. Once our bank service is registered, the bank service’s account operations (add, update, retrieve) are visible and accessible for use within Salesforce with the tool Flow Builder. At this point, we have everything we need to create a flow to automatically add/update an account’s details from the bank service to Salesforce each time the Acme bank representative accesses the account. 

Some of these business actions you’ll be challenged to complete and enhance later, but here’s a sneak peek at the new business process we’re going to build. 

Let’s take a closer look at the stages and logic so you can see how the information flows.

Flow Builder showing 5 elements describing the Bank Service flow

  1. Employee Bank Record Screen: Captures account name and account type from information that the Acme bank representative enters on the screen
  2. Add account Action: Having registered the Acme bank service with External Services, all the actions are now available for use within the flow. We select the addAccount action and specify the inputs and outputs which are assigned to variables for use between the Salesforce org and the backend bank service.
  3. Account updated Decision: Logic evaluates if account exists and needs updating or if it needs creating
  4. Assign updated account: If account exists, assign account id of existing account and then pass to update the user account
  5. Update user account record: If account does not exist, create account and update or if the account already exists, update it

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 schema 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 method that we looked at in the External Services wizard, along with addAccount, DeleteAccount, and updateAccount. These External Services actions are the methods pertaining to our fictional bank that we can use in a flow to find out information, add accounts records, and so forth. External services that you work with can have many more methods; it depends on the specific service that you use and how the schema is defined for your use case.

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

First let’s create two custom fields (Account Type and Account ID) on the User object.

  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. Select Text and 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.

You’ll find out why we need specific fields associated with our bank client but for now 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 and Save.
You can now verify that our user, Maria Thompson, has 2 custom fields (Account Type and Account ID). 

Now let’s get started on our flow.

Create a Flow Using External Services Actions

Ready to get to it? Let’s begin by creating an account record in our fictional bank. The account record is for an employee who has signing authority on one of the organization’s accounts.

Screen
  1. From Setup, enter Flow 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, drag the Screen element onto the canvas.
    • Label: Employee Bank Record
    • API Name: Employee_Bank_Record
  5. From the list of Input screen components, drag two Text components to the screen canvas [Flow Label].
  6. Click the first Text component
    • Label: Name on Account
    • API Name: Name_on_Account
  7. Click the second Text component
    • Label: Account Type
    • API Name: Account_Type
  8. Click Done.
  9. Click Save to save the flow and set the flow’s properties.
    • Flow Label: Employee Bank Record
    • Flow API Name: Employee_Bank_Record
  10. Click Save and ignore any warnings.
Note

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. Now let’s add an element specific to our schema, so that the flow uses the addAccount method to create an account record at our fictional bank. 

Action elements, by definition, perform an action outside of the flow. Selecting the Action element presents and performs invocable actions that were registered by External Services. We’re interested in the action of adding an account.

Action

  1. From the toolbox, 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 Account
    • API Name: Add_Account
  4. Under Set Input Values, select to include
    • accountName
    • accountType
  5. For accountName, enter Name and select Name_on_Account.
  6. For accountType, enter Type and select Account_Type.
  7. Check Manually assign variables (advanced).
  8. Under Store Output Values, for _201, select New Resource
  9. 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
  10. Click Done.
  11. Under Store Output Values for _409 Exception, select New Resource
    • Resource Type: Variable
    • API Name: updatedAccount
    • Data Type: Apex-Defined
    • Apex Class: ExternalService__BankService_accountDetails
    • Availability Outside the Flow: Available for input and Available for output
  12. Click Done.

Flow Builder showing addAccount action inputs and outputs



Now we’re going to add logic for evaluating if the account already exists or not. When the flow executes, if the account doesn’t exist then our flow is routed through the create account path and proceeds on to update the user account. If the account already exists (default outcome), it is routed through update, and we assign it . This type of evaluation and branching logic is perfect for the Decision element. 

Decision
  1. From the Elements tab in the toolbox, drag Decision onto the canvas.
  2. In the New Decision window, enter these values.
    • Label: account updated?
    • API Name: account_updated
  3. In the Outcomes section, notice the two listed outcomes: New Outcome and Default Outcome.
  4. Make sure that New Outcome is selected, and enter these values into the Outcome Details section.
    • Label: account created
    • API Name: account_Created
    • For Condition Requirements to Execute Outcome, select All Conditions Are Met
    • For Resource, enter these conditions.
      Resource Operator Value
      updatedAccount Is Null $GlobalConstant.True
  5. Select Default Outcome and enter:
    • Label: account updated
  6. Click Done.
Note

Note

Notice that "account updated" 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.

Assignment
  1. From the toolbox, drag Assignment onto the canvas.
  2. In the New Assignment window, enter these values.
    • Label: assign updated account
    • API Name: assign_updated_account
  3. For Set Variable Values, enter these values
    Variable Operator Value
    account.id Equals UpdatedAccount.id
  4. Click Done and ignore any warnings
Note

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.

Our screen flow makes use of Apex-defined resources to connect flow to our fictional bank. We need to update our Salesforce user record with the name on the account and the account id. Then we can define two variables to pass between flow and our fictional bank.

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 user account
    • API name: update_user_account
  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: Conditions are Met
    • Field: LastName
    • Operator: Equals
    • Value: 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: Account_Type (under Screen Components)
  8. Click Done.

We also need two record variables for the employee’s user record.

Create Two Record (Single) Variables

  1. From the Toolbox, select the Manager tab.
  2. Click New Resourceand 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 the Start to Screen Employee Bank Record.
  2. Connect the Screen Employee Bank Record to add account Action.
  3. Connect add account Action to Decision account updated?.
  4. Align the Decision element on the canvas so you have space for two outgoing connectors on either side. One connector identifies the “account created” as the element to execute and the other one as “account updated”.
  5. Connect account updated? Decision outcome to Assignment assign updated account.
  6. When prompted, select the account updated for the decision outcome to assign to the path and click Donedecision connector outcome selected for account updated
  7. Connect Assignment assign updated account to Update Records updated user account.
  8. Connect Decision account created to Updated Records update user account.
  9. Click Save.

All Done! Well, not quite ... 

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 Name on Account, type Thompson and for Account Type, enter Purchasing. 

The flow executes with our inputs.

Flow Builder showing debug run of workflow

Under Debug Details, follow the entire flow that you just built. 

For Add Account, notice the name (Thompson) and account type (Purchasing) we entered as inputs. Since the account did not exist prior to this run, it was created.

Note

Note

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

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 Jones for Name on Account, you would have received an error, because a space is illegal in this field. Likewise, an error might display if you enter a name that is less than 6 characters, (Jones), since this is another limit of our simple application.

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 6 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

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! But again, we’re keeping it simple here so that you can focus on External Services.

We can also check if the account was created by using the getAccount method. If you want to try that on your own, you can do that now. However, create another flow to do this. In the challenge, we build on the flow we just finished, so let’s not change it.

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 a schema, 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 Acme bank 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. 

Let's take a look at step 6 and get ready to add steps 7-9 for the Hands-On Challenge.

Canvas with 4 connected items added to previous Bank Service project

Note

Note

If you haven’t already done so, complete the previous Acme 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.

Add an action (step 6) to the flow you created in this unit so that it tracks and validates the creation of the account and the account type. If the account exists, then show the account information to the Acme bank representative. If the account doesn’t exist, we’re going to tell the Acme 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 an account exists and can be confirmed and another variable to cover the case where an account doesn’t exist. 

Create Two Apex-Defined Variables

  1. In the Employee Bank Record flow (from the Manager tab in the toolbox):
  2. 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
    • Availability Outside the Flow: Check both Available for input and Available for output
  5. Click Done
Action
  1. From the toolbox, 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: Check account
    • API Name: Check_account
    • Set accountName: Name_on_Account
    • Check Manually assign variables (advanced)
  4. Under Store Output Values:
    • >_200: CheckedAccount (Apex-Defined Variable)
    • >_404 Exception: notFoundAccount (Apex-Defined Variable)
  5. Click Done
  6. Connect the Update Records element to the Check account Action element.
  7. Save the flow.

Resources

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • 1 in 4 land a new job
  • 50% receive a promotion or raise
  • 80% learn new technologies that boost their resume
  • 66% say it increases productivity
Source: Trailblazer Community Impact Survey 2019