đź“Ł Attention Salesforce Certified Trailblazers! Link your Trailhead and Webassessor accounts and maintain your credentials by December 14th. Learn more.
close
trailhead

Use External Services in a Flow

Learning Objectives

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

  • Find your External Services-generated Apex actions in the Flow palette.
  • Create a simple flow with your External Services-generated Apex actions.

Apex Actions in Flow

If you completed the previous unit, you’ve registered an external service using the External Services Wizard and viewed the generated Apex actions in the wizard. Let’s go into Flow Designer now to see the actions there.
  1. From Setup, enter Flow in the Quick Find box, then select Flows.
  2. Click New Flow to open the Flow Designer.
  3. On the left side of the screen, select the Palette tab and scroll down to the Apex actions section. You can see the Apex actions imported by External Services based on the schema we used in the previous unit.
The Flow Designer with External Services Apex actions visible in the palette.

For example, you can see the getAccount method that we looked at in the External Services wizard, along with addAccount, DeleteAccount, and updateAccount. These are the actions 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 based on your use case.

Stay right where you are. We’re about to use these actions in a flow of our own. First let’s create a custom field on the User object.
  1. From Setup, select Object Manager, then select User.
  2. Click Fields & Relationships, then click New.
  3. Select Text and 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.
You’ll find out why we need this field later.
Let’s create a user record for an employee, Maria Jones:
  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 Jones.
  5. For User License, select Salesforce.
  6. For Profile, select Standard User.
  7. Since this is a test org, use your own email address.
  8. Select a username and nickname to use and save.
Now let’s get started on our flow.

Create a Flow Using External Services-Generated Actions

Ready to get to it? Let’s create 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.

This is a fairly simple flow. Why simple? Because you can learn all about Flow Designer and flows in this module about Lightning Flow. We recommend that you check it out if you haven’t. For now, we want you to see how to use External Services-generated actions in a flow.
  1. From Setup, enter Flow in the Quick Find box, then select Flows and click New Flow to open the Flow Designer.
  2. From the palette, drag the Screen element onto the canvas. You can find it quickly by typing Screen in the search box.
  3. In General Info, type Account Information.
  4. Select the Add a Field tab and drag two text boxes to the right side of the window.
  5. Click the first text box and under general info, type Name on Account.
  6. Click the second text box and under general info, type Account Type and click OK.
  7. Hover over your screen element and select the green arrow to indicate that it’s the starting element.
  8. Click Save.
  9. For Flow Properties use the following:
    1. Name: Employee Bank Record.
    2. Unique Name: Employee_Bank_Record
  10. Click OK. A warning appears, which you can ignore.
So far, we simply added a Flow screen element. Now we add an element specific to our schema. This step in the flow uses the addAccount method to create an account record at our fictional bank.
  1. From the palette, drag the BankService_addAcccount element onto the canvas. You can find it quickly by typing BankService_addAcccount in the search box.
  2. For Name, use Add Employee Record.
  3. Under Inputs, select the accountName parameter and for Source, under Screen Input Fields, select Name_on_Account.
  4. Under Inputs, add one more parameter by clicking Add Row. Select accountType. For Source, use Account_Type, also located under Screen Input Fields.
  5. Select Outputs and for Source, select accountDetails_type_201. For Target, create a variable.
    1. Select CREATE NEW and SObject Variable.
    2. For Unique Name, use accountTypeField.
    3. For Input/Output Type, select Output Only.
    4. For Object type, select Standard > User and click OK.
    5. For Target, select SOBJECT VARIABLES > accountTypeField > Account_Type__c.

Almost there! Make sure that you already set the screen element as the start of your flow and draw a line from it to the one you just created. Save your work.

You can use debug mode to check your flow. Click Debug, select Show details of what's executed and render flow in Lightning runtime, and Run. For Name on Account, type Jones and for Account Type, enter Purchasing. The flow executes with our inputs.
Details after running the flow in debug mode.

Under Outputs, {!accountTypeField.Account_Type__c} = accountDetails_type_201 () reflects that as output, we assigned the flow action for creating an account, accountDetails_type_201, to the variable for account type that we created. We’ll use this output in our challenge to update a Salesforce record.

We could have received an error, too. For example, if we used Maria Jones for Name on Account, we would have received an error as defined in the schema, since a space is illegal in this field.
Note

Note

If we create a flow for actual use for an external banking service, you can imagine that we’d 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 a new flow to do this. In the challenge, we build on the flow we just finished, so let’s not change it.

Summing It All Up

Remember that diagram from the first unit? Here it is again:
Graphic depiction of the External Services process.
We’ve now walked through all the steps. Notice that while we had to deal with a schema, a lot of the work we did was declarative. Nice, huh?
  1. An external services provider, such as a bank, shares its REST-based API schema specification: We got this information from our fictional bank.
  2. Based on the specification, a developer (or maybe even you) creates a schema definition that describes the API: While we didn’t create this schema ourselves, we reviewed the elements of a schema and the requirements for a good one. When you work with external services, you or your developer can define the schema you need for your use case.
  3. A Salesforce administrator declaratively creates a named credential to authenticate to the service’s endpoint using the URL provided by the external service provider: We defined our named credential, https://th-external-services.herokuapp.com.
  4. A Salesforce administrator declaratively registers the service and uses both the named credential and schema definition during the registration process: We just registered our first external service and looked at the generated Apex actions in the wizard.
  5. A Salesforce administrator declaratively creates a flow using the External Services–generated Apex actions: We just created a flow.
  6. Users run the flow: We used the debugger to make sure it runs correctly.
  7. Data is retrieved, created, updated, or deleted: We created an account record using the REST-based api of the service provider. But we aren’t done yet! In the challenge, we also update a Salesforce record using the same flow.
retargeting