How To

Flow Simplification: Replace Loop and Assignments with MapCollection Apex Action

Simplify

I published a previous blog post called “Let’s Get With The Flow – Loop Basics.” This blog post covered putting a bunch of records in a collection, iterating through to update two fields for each record in the collection.

This post will focus on the same use case and will show how in some circumstances, you can use a community built MapCollection apex action from UnofficialSF to update the items in the collection, thus, simplifying the flow by removing the loop and 2 assignment elements.

UnofficialSF is a loose collaboration of bloggers, mvp’s, and the occasional Salesforce employee. You can use this resource to find community built and shared flow screen and action components for use in flow among other great resources on this site. Note: This content is not official Salesforce content so use it at your own risk. The code installed in your org is unmanaged, which means you can customize it accordingly for your org’s purposes. You can access UnofficialSF by this address: https://unofficialsf.com/ .

The installation link provided is for installing the components in Production. However, you should NEVER install components directly in Production. Best practice is that you install these in a sandbox and test it first. If all works out, then install it in Production.

To install in a sandbox, right click the link and select copy link address.

Example: https://login.salesforce.com/packaging/installPackage.apexp?p0=04t1K0000033LMX

Take the “/packaging/installPackage.apexp?p0=04t1K0000033LMX” part of the URL and paste it at the end of your sandbox address.

Sandbox address example: https://jenwlee–dev.my.salesforce.com

Combined URL is: https://jenwlee–dev.my.salesforce.com/packaging/installPackage.apexp?p0=04t1K0000033LMX

This URL will take you to the package installation for your sandbox.

The MapCollection apex action takes a collection and updates the each field set with a name matching the key to the specified value.

Example: Rating : Warm, Industry : Banking

A couple of caveats from the testing that I’ve done:

  • Doesn’t work on checkboxes (boolean)
  • It doesn’t update a value that contains a space, such as “New York”

Here are a few lessons learned from implementing this use case:

  • Use the community built MapCollection apex action to replace the loop and 2 assignments.
  • Provide descriptions, where provided, in Salesforce. This may be tedious step, I know, but your future self will thank you when you are trying to remember what you configured or assist other/future admins when troubleshooting or enhancing what was built. This includes variables, the purpose of a flow, what each flow element does, etc.

Business Use Case:  Addison Dogster is the system administrator at Universal Containers. Mary Markle is the Director of Operations. She would like to automatically update cases with a status of Escalated to a High priority and set the IsEscalated checkbox to true.

Solution: This blog post will just cover solution of getting the group of cases that have the Escalated status and taking action on those records.

It replaces the following in the original flow:

OriginalFlow.PNG

The new flow looks like this:

FlowWithMapCollection.PNG

This flow does the following: (1) get the cases where the status is set to Escalated and store the records in a collection, (2) a decision to determine whether records were found in the query, (3) set the priority to “High” for each record in the collection and (4) updates the records in the new collection.

General Steps: 

1. Install the MapCollection apex action in your sandbox.

2. Let’s create the flow. For those using Salesforce Classic, flow can be found in Create | Workflows & Approvals | Flows. In Lightning Experience, it is found under Process Automation | Flows.

A. Next, we will use Get Records flow element to query the Case object to get all the records with the status of Escalated.

Object: Case

Filter the Case Records by Status Equals Escalated.

How many records to store: All records

For the “How to Store the Data” option, I chose to use “Automatically store all fields.”

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

 

FlowWithaLoop-GetRecords.PNG

B. We will a Decision flow element to determine whether records were found in the previous step. If not records were found, the flow would fault when it get to the step in the flow where it tries to do things with the loop record variable — there is no record to work with!

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

For the “Records Found” outcome, we need to check record collection has items. Set the conditions to the following:

{!Get_Contacts} Is Null {!GlobalConstant.False} (This is a double negative. Which means, there is at least one record in the collection).

The default outcome: No Records

FlowWithaLoop-Decision.PNGView image full screen

C. Next, drag the Action element to the Canvas. Search for the MapCollection action. Configure it as follows:

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Object for “inputCollection” (Input): Case

Object for “outputCollection” (Output): Case

inputCollection: {!Get_Contacts} [This is the output from the Get Records element]

keyValuePairs: Priority: High [Format is field API name: value. If you are referencing a custom field, make sure it ends in “__c” followed by a colon and the value you are setting it too]

FlowWithMapCollection-MapCollectionApexAction.PNGView image full screen

D. We now need to update all the records in the collection from the MapCollection apex action {!Update_the_collection.outputCollection} with an Update Records flow element. This will update the priority to “High” for all records in the collection.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure it as follows:

How to Find Records to Update and Set Their Values: Use the IDs and all field values from a record or record collection

Record or Record Collection: {!Update_the_collection.outputCollection}

FlowWithMapCollection-UpdateRecords.PNGView image full screen

E. Add the subflow Send Flow Fault Email. For instructions on how to create this, go to Step 2 of blog post: Maximize Maintainability With Process Builder and Componentized Visual Workflow. Set your flow starting point. And connect the flow elements, fault connectors and outcome connectors to match the below…

FlowWithMapCollection-Connectors.PNG

F. Save/Save As and provide the following properties.

Best practice tip: Provide a description so you and other/future admins know what this flow is used for.

FlowWithMapCollection-Properties.PNGView image full screen

G. Click the “Activate” button.

Note: This flow can be set as a scheduled flow or invoked by a process or invoked by a button. The initiation will not be covered in this blog post.

Now, before you deploy the changes to Production, don’t forget to test your configuration changes.

  1. Create a few cases with the status of Escalated with a priority that is not set to High.
  2. Initiate the flow.
  3. Verify the cases were updated (priority is now set to High).

Deployment Notes/Tips:

  • Flow and MapCollection components can be deployed to Production in a change set (or can be deployed using a tool such as Metazoa’s Snapshot).
  • You will find flow in a change set under the Flow Definition component type.
  • Activate the flow post deployment as flows deploy inactive in Production, unless you have opted in on the Process Automation Settings screen, to “Deploy processes and flows as active.” NOTE: With this change, in order to successfully deploy a process or flow, your org’s Apex tests must launch at least 75% of the total number of active processes and active autolaunched flows in your org.