+
+

Lab 2: Calling the new External Service from Salesforce Flow

Overview

In this lab we’ll be building a Salesforce Flow that uses the Database System API we developed and deployed in Module 1. Use case: when a new Contact is created in Salesforce, determine some logic, add this account to the Database and link the Id from the Database to the Database_Id__c field on the Contact.

Step 1: Create a new flow

  1. From the setup page, click Home

  2. In the Quick Find search bar, type flows

  3. Under Process Automation, click Flows

  4. Click New Flow

    create new flow

  5. Select Record-Triggered Flow and click Create.

    select record triggered

  6. On the Configure Start screen, enter the following details:

    • Object: Contact

    • Trigger the Flow When: A record is created

      create flow details1

    • Condition Requirements: None

    • Optimize the Flow for: Actions and Related Records

    • Check the box next to Include a Run Asynchronously path to access an external system after the original transaction for the triggering record is successfully committed

  7. Click Done

    create flow details2

  8. Change the layout type to Free-Form

    flow set free form

Step 2: Add the Database SAPI external service to the flow

The Toolbox on the left side of the screen contains the elements that can be used in a flow. To reference our new external service, we will use the Action element from the Interaction section.

  1. Click and drag the Action Interaction element onto the canvas.

  2. Change the Filter By drop to Type

  3. Select External Service

  4. Click the Action search bar and select the postCustomer action

    select action

  5. Enter the following details:

    • Label: Add customer to database

    • API Name: Add_customer_to_database (auto-filled)

    • Description: Add a customer record to the external database

      new action details

  6. Under Set Input Values, click the toggle next to the generated input.

    create flow action set input values

  7. Click the search bar to assign a resource to the input.

  8. Click New Resource

    set action input

  9. Enter the following details:

    • Reource Type: Variable

    • API Name: databaseCustomer

    • Data Type: Apex-Defined

    • Apex Class: ExternalService__databaseSapi_customer

    • Available for input: true

    • Available for output: true

      create customer variable details

  10. Click Done.

  11. Click the arrow next to Advanced to open the advanced options.

  12. Check the box next to Manually assign variables.

  13. Click the search bar under the 200 output value.

    set action output1

  14. Click New Resource and enter the following details:

    • Resource Type: Variable

    • API Name: databaseResponse

    • Data Type: Apex-Defined

    • Apex Class: ExternalService__databaseSapi_postCustomer_OUT_200

    • Available for input: true

    • Available for output: true

      create action output var

      set action output2

  15. Click Done and Done

Step 3: Add business logic

Note: Before proceeding with Step 3, please ensure you have completed step 2.3 in the prerequisites module. Prerequisite step 2.3 walks through creating a new custom field on the Contact object, which we will use to store the Database Id returned by our API as an external Id on the Contact record. If you haven’t completed this step please return to Prerequisites and complete step 2.3.

With the action configured on the canvas, the next step is to extract the relevant record details and add them to the variable we have created that represents the API input payload.

  1. Click and drag the Get Records Data element onto the canvas.

  2. Enter the following details:

    • Label: Get contact details

    • API Name: Get_contact_details (auto-filled)

    • Object: Contact

    • Condition Requirements: All Condition Are Met (AND)

    • Field: Id

    • Operator: Equals

    • Value: $Record.Id

      Note: This value can be selected by clicking the search bar, scrolling down to Global Variables, clicking "Record" and "Id".

      get records details

      set record global variable

      set record id

  3. Click Done

  4. Click and drag the Assignment Logic element onto the canvas.

  5. Enter the following details:

    • Name: Map contact to customer

    • API Name: Map_contact_to_customer (auto-fill)

      assign customer variable details1

      Now we need to assign the contact details to the relevant fields of the databaseCustomer variable we created in Step 2.

  6. Click the Search variables search bar

  7. Select the databaseCustomer apex-defined variable

    customer variable set field1 customer

  8. Select the firstName field

    assign customer variable set field2

  9. Set the operator to Equals

  10. Click the Enter value or search resources search bar

  11. Select the Contact from Get_contact_details Record (Single) Variable

    customer variable set field1 contact

  12. Select the FirstName field

    set contact first name

  13. Click Add Assignment and repeat steps 6 - 12 for the following fields:

    • databaseCustomer.lastName Equals Get_contact_details.LastName

    • databaseCustomer.email Equals Get_contact_details.Email

    • databaseCustomer.company Equals Get_contact_details.Account.Name

      assign customer variable details2

  14. Click Done

    The final element to be added is to use the response from the external service to update the contact record we created.

  15. Click and drag the Update Records Data element onto the canvas and enter the following details:

    • Label: Add database Id to Contact

    • API Name: Add_database_Id_to_Contact

    • How to Find Records to Update and Set Their Values: Use the contact record that triggered the flow

    • Condition Requirements to Update Record: None-Always Update Record

      update database id  c1

    • Field: Database_Id__c

    • Value: databaseResponse.id

      update database id  c2

  16. Click Done

  17. Connect the start element to the Get contact details element

  18. Select the Run Asynchronously path

    select async

  19. Connect the rest of the configured elements in the following order:

    • Get contact details → Map contact to customer → Add customer to database → Add database Id to Contact

    • Your final flow should look similar to this.

      final flow

  20. Click Save and name the flow:

    • Flow label: Database Sync on New Contact

    • Flow API Name: Database_Sync_on_New_Contact

  21. Click Save

Step 4: Test and activate

Before Activating the flow, we will use the debug window to test the functionality of our flow.

  1. Click Debug

    click debug

  2. Ensure the Path for Debug run is set to Run Asynchronously

  3. Select Andy Young as the Contact

    Note: Andy Young should simply be the first contact to appear in the list, you can select any Contact available in your environment for this step.

    debug input

  4. Click Run

    The Debug Details on the right side of the window let you see the inputs and outputs of each element and provides details about actions taken in Salesforce.

  5. Expand the databaseSapi action details

    • You should see Andy’s details populating the "inputs" section and the databaseId in the output.

      debug action detail

  6. Expand the Update Records details.

    • Here you can see a summary of the updates applied to the record. Notably, the Database_Id__c field has been set to the value of the databaseSapi output.

      debug update record detail

  7. Having tested the flow with the debugger, we can now activate it by clicking the Activate button

    activate flow

  8. Exit the Flow Builder by clicking the Back arrow in the top left of the window. flow back arrow

  9. Navigate to the Service app by clicking the App Launcher icon and clicking Service

    navigate service app

  10. Click Contacts

    create new contact

  11. Click New to create a new contact, entering the following details:

    • First Name: Lauren

    • Last Name: Bailey

    • Account: United Oil & Gas Corp.

    • Email: lbailey@example.com

      create contact details

  12. Click Save

  13. When the record page loads, click into the Details tab

  14. Scroll down to the Database Id field and notice that it has been populated automatically.

    created contact with dbid

Note: Since we are making a call to an external service, there may be some delay before the flow is able to run, recieve a response and update the record. If the field hasn’t populated yet, wait a few moments and reload the page. The flow should run within a few minutes.

Summary

In this module you completed these steps to create a flow, automatically triggered by the creation of a record in Salesforce, that leverages the Database API you created in Module 1 and automatically synced the data returned by the API back to the original record. You also performed some simple debugging to very quickly and easily test and validate the functionality of your flow.

Congratulations! You have successfully made excellent use of a MuleSoft API, designed and built by you, from your Salesforce Org without writing a single line of code!

Please proceed to Module 3

Submit your feedback!
Share your thoughts to help us build the best workshop experience for you!
Take our latest survey!