Join the Lightning Side of the Force! DCS Lightning Experience!

Advanced Flow Tips and Tricks

By now, all administrators and developers should be familiar with Salesforce’s Flow capability. In brief, it’s a visual way of outlining and implementing a business process or set of tasks.  Flows are a great way for both administrators and developers to customize Salesforce to suit their business’ needs.

With the ability to interact upon record creation or update (Headless Flows), through a series of screens and interviews (Visual Flows), when a user logs in (Authentication flows), or when a user is created in Salesforce (User Provisioning Flows), Flows can range from the simplest to most complex of requirements.

In this blog post, we’ll discuss a few tips and tricks for managing some of the more complex situations you may come across in Flows.

Cloning Related Records Using Loops, Collections, and Assignments

The first flow tip is for cloning related records in a Flow. Before Flow was introduced, developers would need to write a trigger and test class for cloning related records.  However, Flow now gives the capability to do this declaratively.

To clone related records, you’ll use a Fast Lookup element to look at related records, and use the parent relationship as the filter. In the example below, I’m passing in the CampaignId variable from a Process that fires on Campaign update, which is an Input only sObject variable in my Flow:

Figure1

Next, you’ll hook the Fast Lookup element to a loop and a series of assignment records before performing a Fast Update. The Flow should resemble the following figure:

Figure2

The first assignment element in the loop assigns all of the necessary values from the current cloned job (as specified by the loop element) into a new sObject variable. This prevents fields like the Id from being copied over, which would throw Fault errors.  The second assignment element “assigns” or adds the new sObject variable to a new collection of sObjects.  Last, a Fast Create element is used for inserting the new collection of sObjects.

Using declarative tools like formulas in your Flow

There you have it, a simple elegant flow for cloning related records. However, what if you want to take the cloning a step further and clone related records of related records?

Taking a step back, we’ll need to rethink our flow a bit. The related records’ relationships will need to be generated dynamically and point to the new records that were created in the Fast Create element.  This introduces a few challenges:

  • How do we query for the grand children records that pertain to only the original Campaign record?
  • How do we link the grand children records to their newly cloned parents?

The answer lies in formulas and external Ids. First, I want to create an external id or text field on the “child” record, or in the example above, Volunteer Jobs. This field will store a temporary value containing the id of the originating record. Second, create a formula field on the grand child record that brings in the grand parent relationship. The formula field prevents the need from a weird relationship from the grand child to the campaign, but still allows us to filter on the grand children for the second fast lookup.

The formula should be a text value and look like Parent__r.Parent__c, accessing the Id of grand parent relationship.

More formulae action

Now, let’s go ahead and chain the Fast Update to another Fast Lookup for the grand children. For the filter fields, use the “Parent Campaign” formula field mentioned earlier. The Fast Lookup configuration should resemble the following:  

Figure3

Now, what happens if we run the Fast Lookup as is? You’ll notice that the fast lookup returns 0 records. The reason being? Parent_Campaign__c and CampaignId are different formats of the same Id field. Instead, you’ll need to go back and update the Parent_Campaign__c field and surround it with the CASESAFEID function CASESAFEID(Parent__r.Parent__c) and now, your Fast Lookup should work.

Putting it all together

Now that we’ve discovered how to utilize flows, collections, loops, and assignments, let’s finish implementing the cloning of the grand children, or Volunteer Skills in this example. In particular, we will need the following elements:

  1. A Fast Lookup element for querying the Volunteer Skills to clone, as shown previously
  2. A Loop element to loop through the original grand children.
  3. An Assignment element to assign the old Volunteer Skill values to a new sObject variable. (Omit the Parent relationship from this element).
  4. A Loop element for looping through the cloned Volunteer Jobs (or parents of the grand children in step 1).
  5. A decision element, which looks for the original id of the cloned Volunteer Job (we’ve already copied this value into an external id field on the volunteer job object within the first loop)
  6. An assignment element to copy over the Volunteer Job’s id field to the Volunteer Skill’s relationship field.
  7. After the inner loop is complete, an assignment element adds the new Volunteer Skill sObject variable to a sObject collection variable of Volunteer Shift records
  8. A Fast Update element to update that collection of Volunteer Shift records.

Tying all the elements together, the flow should resemble the following design:

Figure4

After saving the flow and hooking it up to a process or screen, the flow will clone any related volunteer jobs and any of the volunteer job’s related skills.

When a Flow Throws an Exception

Now that we’ve developed a flow, it still needs to be tested. However, what if something goes awry? What if a fault or exception is thrown? Unfortunately, this is one area that Flow is notoriously lacking in. Most exceptions are difficult, perhaps impossible to read and understand.

When this is the case, it’s time to take apart the flow by pieces and do a little guess and check work. It may even help at this point to bring an InvocableMethod to help debug your flow.

public with sharing class InvocableLogger {

@InvocableMethod  

public static void log(List<String> messages){               

System.debug(messages);

}

}

This simple 5 line Apex class uses the InvocableMethod annotation, allowing it to be used in Flows, and writes any input to the Debug Log in Salesforce. To utilize the InvocableLogger

  • Copy and Paste the Apex code above into a new Apex Class (Setup->Develop->Apex Classes->New)
  • Open / Edit the flow.
  • Drag the InvocableLogger over from the Apex section on the Flow Palette on the Sidebar.
  • Specify either a text value, template or variable for the messages input on the Invocable Configuration screen:

Figure5

  • Specify either a text template or a variable for the messages input on the InvocableLogger configuration screen:
  • Plug the InvocableLogger element into your flow where desired, such as in the fault paths within your flow:

Figure6

To see the debug logs from the InvocableLogger, turn on Debug Logs for the running user (Setup->Monitoring->Debug Logs in Salesforce), and run the flow, and then look for the messages in the logs. Although this will not fix the fault exceptions, it will help track down the root cause of any exceptions, if used properly.

Written by James Loghry, Demand Chain Systems – Salesforce.com Architect and Salesforce MVP /Force.com MVP

James is an accomplished professional with over ten years ‘experience in the IT industry. Both a Salesforce MVP and Force.com MVP, James is certified in administration, advanced administration, development and advanced development.


What do you think of these Flow tips? Do you have any you’d like to share?

Tweet them and mention us, @DemandChainSystems!

Additional information on DCS Salesforce Development available on our Salesforce Development page.

Also, if you’re interested in learning more about our Salesforce admin services or consulting, please contact us at info@demandchainsystems.com.

 

Facebooktwittergoogle_plusredditpinterestlinkedinby feather
Facebooktwitterlinkedinby feather

No Comment

Comments are closed.

You may also like

Lube-Tech

Case Study: Lubrication Technologies Lubrication Technologies, Inc. (Lube-Tech) is an innovative lubricants company offering a broad ...
Read More
Salesforce DX

Salesforce DX: What is it and how do I prepare for it? Salesforce DX, arguably the ...
Read More