+
+

Lab 5: Invoke Published Document Action using Salesforce Flow

Overview

In this lab you’ll learn how to invoke from Salesforce Flow an IDP Document Action previously published to Anypoint Exchange.

As part of the exercise you’ll start from a couple of pre-drafted flows and you’ll be asked to complete it. At the end of the lab you will be able to attach an invoice to an account and have an invoice record created in salesforce (along with line items) using data extracted by IDP.

Prerequisites

If you didn’t complete Lab 1: Process an Invoice with MuleSoft IDP , in order to be able to consume the NTO Invoice Document Action in Anypoint Studio, you have two options:

  • Use the document action published by one of the other workshop attendee.

  • Quickly create a new <your-initials> - NTO Invoice document action (leave everything set to the default) and publish it to exchange.

Step 1: Clone the Pre-drafted Autolaunched Flow to Post Document to IDP

  1. Login into the Salesforce Developer ORG and access the Setup menù.

    module g  setup menu
  2. Search for Flows in the Quick Find text field and click Flows under Process Automation.

    module g  quick find flows
  3. Select the IDP Lab’s Flows list view from the drop down menu in the top left section.

    module g  flow lab list view
    module g  idp flows list view
  4. Click the LAB - RECT - IDP - Post Document (Draft) flow label link from the list.

    module g  idp post doc draft
  5. Once the flow is opened click the Save As button on the top right.

    module g  save as flow
  6. Select Save as A New Flow and provide <your-initials> - LAB - RECT - IDP - Post Document as Flow Label. Click outside the label field and the Flow API Name should appear automatically.

    Click the Save button.

    module g  save as new flow modal post doc

Step 2: Complete the Flow to Post Document to IDP

You’ll now complete the pre drafted flow in charge of posting the uploaded file (invoice) to IDP. The flow is triggered when the account is updated to reflect that the file has been attached to the account.

Following you see the pre-drafted flow.

module g  post doc draft flow start
  1. Left-click on the decision element.

    module g  flow post doc decision ele
  2. Left-click on the Edit Element Button.

    module g  flow edit dec element
  3. On the right-side of the screen you see the decision element configuration. You need to update the condition to execute the Yes outcome. Replace the condition Value with <your-initials> -

    module g  flow edit dec element modal
  4. Left-click on the assignment element.

    module g  flow post doc ass ele
  5. Left-click on the Edit Element Button.

    module g  flow edit ass element
  6. On the right-side of the screen you see the assignment element configuration. Configure the variables as follow:

    Variable Operator Value

    {!requestPayload.origin}

    Equals

    Salesforce

    {!requestPayload.originId}

    Equals

    {!$Record.Id}

    {!requestPayload.originFileContentId}

    Equals

    {!Get_Content_Version.Id}

    {!requestPayload.originCallbackDestination}

    Equals

    IDPCallback__e

    module g  flow edit ass element modal
  7. Click the Add Element button right after the Set Request Payload assignment element

    module g  add element btn
  8. In search filed enter postDocument and select the first result.

    module g  flow add element post action
  9. Before we configure the postDocument action we need to collect some information for the Document Action published within Anypoint Exchange.

    module g  exchange doc act asset
  10. Open the POST method for your Document Action published to Exchange. From the endpoint you need to take note of the following information:

    • API Version (eg. v1)

    • ORG Id (eg. c1f72f83-cfad-43cd-8a31-b48347945466)

    • Action ID (eg. 7ab01641-a76d-4348-9307-2fbe8ab93434)

    • Action Version (eg. 1.0.0)

      module g  post endpoint elements

      !!! Important Note !!! The values above are just an example and might differ from the one you should use.

  11. Go back to flow builder in Salesforce. You need now to properly configure the postDocument action. Enter Post Document to IDP as label.

    Configure input values for the action as follows:

    Attribute Value

    Action Id

    <Replace with value from Anypoint Exchange>

    Action Version

    <Replace with value from Anypoint Exchange>

    API Version

    <Replace with value from Anypoint Exchange>

    Mode

    fireAndForget

    ORG Id

    <Replace with value from Anypoint Exchange>

    Generated (Enable the Included switch)

    {!requestPayload}

    module g  flow post action config
  12. Click Save button.

    module g  save btn
  13. Click Activate button.

    module g  activate btn

Step 3: Connect Salesforce to Anypoint Exchange

  1. Access the Setup menù.

    module g  setup menu
  2. Search for MuleSoft in the Quick Find text field and click Services under MuleSoft.

    module g  ms services menu
  3. At this point you could have the connection with Anypoint Platform already in place or not. In case the connection is already active you will see something like follows with the message "Logged in to MuleSoft Anypoint Platform as <username>". If this is the case you can skip the following instructions by clicking here.

    module g  already logged in into ms anypoint platform

    In case the connection is not in place this will be done by the first attendee or by the instructor once for everyone. Click on the Log In button.

    module g  login in into ms anypoint platform
  4. Leave US Cloud as selected option for the control plane and click Login In.

    module g  login in into ms anypoint platform modal
  5. Enter user credentials you previously used to log into Anypoint Platform.

    module g  anypoint platform login
  6. Grant Salesforce the access to Anypoint Platform with the provided credentials.

    module g  anypoint allow

Step 4: Import the Document Action into Salesforce from Anypoint Exchange

  1. Now that the connection is correctly setup, click the Import button to import the document action you previously published to exchange.

    module g  import btn
  2. Select from the list the service with your initials (<your-intials> NTO Invoice) and click Next.

    module g  import service modal

    Configure the service as follows.

    Field Value

    Name

    <your-intials>NTOInvoice100 (eg. TBNTOInvoice100)

    Description

    MuleSoft <your-intials> - NTO Invoice 1.0.0

    Named Credential

    <Select MuleSoft_IDP_US>

    Click Save & Next.

    module g  import service modal 2
  3. Select application/json as Supported Media Type and click Next

    module g  import service modal 3
  4. Select the getDocumentActionExecution Operation and click Finish.

    module g  import service modal 4
  5. You should now see your service among the available ones.

    module g  imported service entry

Step 5: Clone the Pre-drafted Platform Triggered Flow to Get IDP Document Results

  1. Login into the Salesforce Developer ORG and access the Setup menù.

    module g  setup menu
  2. Search for Flows in the Quick Find text field and click Flows under Process Automation.

    module g  quick find flows
  3. Select the IDP Lab’s Flows list view from the drop down menu in the top left section.

    module g  flow lab list view
    module g  idp flows list view
  4. Click the LAB - RECT - IDP - Get Document (Draft) flow label link from the list.

    module g  idp get doc draft
  5. Once the flow is opened click the Save As button on the top right.

    module g  save as flow
  6. Select Save as A New Flow and provide <your-initials> - LAB - RECT - IDP - Get Document as Flow Label. Click outside the label field and the Flow API Name should appear automatically.

    Click the Save button.

    module g  save as new flow modal get doc

Step 6: Complete the Flow to Get IDP Document Results

You’ll now complete the pre drafted flow in charge of getting the data extracted by IDP and map it to Salesforce Objects. The flow is triggered when the account is updated after receiving a Platform Event from Anypoint.

Following you see the pre-drafted flow.

module g  get doc draft flow start
  1. Left-click on the Start element to expand it.

    module g  start element
  2. Left-click on the Edit link to update the flow triggering conditions.

    module g  start element expanded
  3. Update the second Entry Condition, the one checking the prefix for the Account Name and replace the value with <your-initials> -

    module g  entry conditions config
  4. You will now create a new variable to store the response returned by IDP when posting the document. Click the New Resource button in the top-left corner in the toolbox panel.

    module g  new resource btn
  5. Configure the New Resource as follows.

    Field Value

    Resource Type

    Variable

    API Name

    responsePayload

    Data Type

    Apex-Defined

    Apex Class

    ExternalService__<your-initials>NTOInvoice100_DocumentExecutionGetResponse

    Click Done button.

    module g  craete response var modal
  6. Click the Add Element button right after the Set Execution Id assignment element

    module g  add element btn
  7. In the search text box look for Get Document Action Execution, then select the first result.

    module g  flow add element get action
  8. You can now configured the Get Document Action Execution element as follows.

    Field Value

    Label

    Get Document Action Execution

    API Name

    Get_Document_Action_Execution

    Set Input Values for the Selected Action → Execution Id

    {!executionId}

    Show advanced options → Manually assign variables

    true

    Show advanced options → Store Output Values → 200

    {!responsePayload}

    module g  flow get action config
  9. Click the Add Element button right after the Get Document Action Execution action element

    module g  add element btn
  10. In the search text box look for Loop, then select the first result.

    module g  flow add element loop
  11. You can now configured the Loop element as follows.

    Field Value

    Label

    Loop Over Pages

    API Name

    Loop_Over_Pages

    Collection Variable

    {!responsePayload.pages}

    Direction

    First item to last item

    module g  flow loop config
  12. Click the Add Element button inside the loop right after the "For Each" label.

    module g  add element btn
  13. In the search text box look for Transform, then select the first result.

    module g  flow add element transf
  14. Set Map to Invoice__c as label and Map_to_Invoice__c as API Name.

    module g  flow transf inv config
  15. Let’s now define the source data for our invoice. Click the + button right to "Source Data".

    module g  add source data btn
  16. In the search text box enter Loop and click the first result. Doing so we are adding the page data extracted by IDP as first data source.

    module g  add source data page
  17. We need now to add as second data source the account record, this is required since we need to link our invoice to the related account.

    In the search text box enter Account and click the first result under Global Variables.

    module g  add source data acc
  18. Now you have added the required data sources and you should see on the left something like follows.

    module g  source data added invoice
  19. Let’s now define the target data for our invoice. Click the + button right to "Target Data".

    module g  add target data btn
  20. Select Record as Data Type and in the Object text box look for Invoice\\__c

    module g  add target data inv
  21. Now you have added the required data target and you should see on the right something like follows.

    module g  target data added invoice
  22. In order to map a source data field to a target data field you need at first to click the map icon right to the source data field.

    module g  select source value
  23. Then you need to select map icon left to the target data field you want to map to.

    module g  inv map target value
  24. As a result you should see link connecting the two fields as follows.

    module g  inv mapped value
  25. Map the fields as follows.

    Source Data Field Target Data Field

    Loop Over Pages → fields → dueDate → value

    Invoice__c.DueDate__c

    Loop Over Pages → fields → invoiceDate → value

    Invoice__c.InvoiceDate__c

    Loop Over Pages → fields → invoiceNumber → value

    Invoice__c.InvoiceNumber__c

    Loop Over Pages → fields → subtotal → value

    Invoice__c.TotalAmount__c

    Loop Over Pages → fields → total → value

    Invoice__c.TotalAmountWithTax__c

    Loop Over Pages → fields → tax → value

    Invoice__c.TotalTaxAmount__c

    $Record.Id

    Invoice__c.BillAccount__c

    module g  inv mapped values
  26. Click the Add Element button right after the "Map to Invoice__c" transform element.

    module g  add element btn
  27. In the search text box look for Create Records, then select the first result.

    module g  flow add element create rec
  28. You can now configured the Create Records element as follows.

    Field Value

    Label

    Create Invoice__c

    API Name

    Create_Invoice__c

    How to set record field values

    From a Record Variable

    How Many Records to Create

    One

    Record

    {!Map_to_Invoice__c}

    module g  flow create inv config
  29. Click the Add Element button right after the "Create Invoice__c" create records element.

    module g  add element btn
  30. In the search text box look for Transform, then select the first result.

    module g  flow add element transf
  31. Set Map to InvoiceLine__c as label and Map_to_InvoiceLine__c as API Name.

    module g  flow transf invitems config
  32. Let’s now define the source data for our invoice. Click the + button right to "Source Data".

    module g  add source data btn
  33. In the search text box enter Loop and click the first result. Doing so we are adding the page data extracted by IDP as first data source.

    module g  add source data page
  34. We need now to add as second data source the invoice record you just created, this is required since we need to link our invoice line to the related invoice.

    In the search text box enter Map_to_Invoice__c and click the first result under RECORD (SINGLE) VARIABLES.

    module g  add source data invoice
  35. Now you have added the required data sources and you should see on the left something like follows.

    module g  source data added invoiceline
  36. Let’s now define the target data for our invoice line. Click the + button right to "Target Data".

    module g  add target data btn
  37. Select Record as Data Type and in the Object text box look for InvoiceLine__c

    module g  add target data invitems
  38. Now you have added the required data target and you should see on the right something like follows.

    module g  target data added invoiceitems
  39. Map the fields as follows.

    Source Data Field Target Data Field

    Loop Over Pages → tables → table1 → description → value

    InvoiceLine__c.Description__c

    Loop Over Pages → tables → table1 → price → value

    InvoiceLine__c.LineAmount__c

    Loop Over Pages → tables → table1 → quantity → value

    InvoiceLine__c.Quantity__c

    Loop Over Pages → tables → table1 → unitPrice → value

    InvoiceLine__c.UnitPrice__c

    Map to Invoice__c → Id

    InvoiceLine__c.Invoice__c

    module g  invitems mapped values
  40. Click the Add Element button right after the "Map to InvoiceLine__c" transform element.

    module g  add element btn
  41. In the search text box look for Create Records, then select the first result.

    module g  flow add element create rec
  42. You can now configured the Create Records element as follows.

    Field Value

    Label

    Create InvoiceLine__c

    API Name

    Create_InvoiceLine__c

    How to set record field values

    From a Record Variable

    How Many Records to Create

    Multiple

    Record

    {!Map_to_InvoiceLine__c}

    module g  flow create invitems config
  43. Click the Save button in the top-right corner.

    module g  save btn
  44. Activate the flow by cliccking the Activate button in the top-right corner.

    module g  activate btn

Step 7: Test Flows

You’ll now test what configured so far.

  1. Open the App launcher from the top-left corner within Salesforce and search for MuleSoft IDP, then select the first result to open the App.

    module g  app launcher mod idp
  2. Navigate to the Accounts by clicking the related tab.

    module g  account tab
  3. Click the New button to create a new Account.

    module g  new btn
  4. Enter <your-initials> - Northern Trail Outfitters as Account Name then click the Save button.

    module g  new account wiz
  5. You should now be on the newly created Account page. Click the Upload Files button in the Files panel located in the top-right corner.

    module g  upload files btn
  6. Using the system file explorer navigate to the folder containing the NTO invoice and select it. Click the Open button.

    !!! - important - The file name must have you initials as prefix (eg. TB - …​) !!!

    module g  select file modal
  7. Click the Done button.

    module g  file upload done
  8. Once the document is posted to IDP you should recieve a notification as follows.

    module g  invk flow test bell notif
  9. Let’s open the notification by clicking on the bell icon. The notification inform you that the document has been properly posted to IDP.

    module g  invk flow test notif sent to idp
  10. Now you need to wait for IDP to complete the document processing and the second flow you built to run and map all the extracted data to Salesforce object. Let’s wait for the notification.

    module g  invk flow test bell notif
  11. Let’s open the notification by clicking on the bell icon. The notification inform you that an invoice record is created within Salesforce.

    module g  invk flow test notif inv created
  12. Now that the invoice has been create you should see it on the Account page in the Invoice Panel right below the uploaded files. Click on the invoice code to open the invoice detail page.

    module g  invk flow test invoice panel
  13. As you can see the invoice has been successfully created along with the related line items.

    module g  invk flow test invoice record
Submit your feedback!
Share your thoughts to help us build the best workshop experience for you!
Take our latest survey!