My name is Kanav Khurana and I’m a Senior Solution Architect within Nextview. Recently I have been working more and more with Salesforce flows. The use of Flows can automate multiple processes within the business. Flows have given functional consultants a new superpower: complex business logic – that earlier needed Apex code – can now be automated declaratively. I have recently started using them extensively and would like to share my top 5 learnings to help you embark on the ‘very fulfilling’ journey of building Flows.

Do Not Overstretch

 

 

 

 

 

 

 

If you have ever written or are going to write a moderately complex flow, you may come across this exception: Number of iterations exceeded”. 

As mentioned in the Salesforce official documentation,  the maximum number of executed elements at runtime is 2000. An ‘element’ refers to any block – Query, Screen, Action, Loop, Assignment etc. – that you can drag and drop to the Flow slate from the ‘Toolbox’ -> ‘Elements’ tab on the left. 

Let us look at this in the context of a flow:

 

 

 

 

 

 

 

 

 

This a flow that accepts a list of Accounts and loops through them to check for the Industry – and based on the Industry – assigns an Owner. Once the loop is complete, the Account records are updated. 

If we run this flow for 200 accounts, then the elements that form the loop portion (highlighted in pink) will count towards 200 * 2 = 400 of the 2000 elements. As you can see, this limit of 2000 elements can run out quickly when looping through multiple records/variables. In such cases, it is best to offload the iterative logic to Apex:

 

 

 

 

 

 

 

 

This flow consumes 2 elements out of the 2000 irrespective of the number of Accounts being processed.

Connect to external systems

Yes, you heard that right! Until now, functional consultants had to rely on Workflow Outbound Messages to send data from Salesforce to external systems declaratively. However, Outbound messages come with their set of limitations.

A nifty alternative is to use External Services which let you connect to External systems (mostly) declaratively via Flows. Learn more about this feature in this trailhead.

Run flows in System Mode

A recent addition to the Flow arsenal is the ability to run flows in System Mode. This is especially useful for Screen flows which (by default) run in the logged-in User’s context. 

It is sometimes necessary to write back-end automation where it is not intended to give associated profile rights to the executing user. In such cases, ‘System mode’ comes to the rescue. 

This setting can be enabled via the following steps:

 

Step 1. Go to the settings in the Flow builder via the gear icon

 

 

 

 

Step 2. Click ‘Show Advanced’ 

 

 

 

 

Step 3. Under ‘How to Run the Flow’, select ‘System Context with Sharing – Enforces Record-Level Access’ and then click ‘Done’.

 

 

 

 

 

Make flows test-ready

 

One of the downsides to writing Screen flows is the inability to write automated Apex tests.

Apex testing has many benefits including reducing manual testing time, reducing regression testing and ensuring product consistency.

However, there is a way to get around this by re-packaging the flow. 

Consider a screen flow that calculates the credit rating for an Account based on the Account Industry:

 

 

 

 

 

This flow can be broken down into 2 parts – a screen flow with the ‘User Input’ portion – and an autolaunched sub-flow with the Core Logic’:

Part 1: Screen flow 

 

 

 

 

Part 2: Autolaunched sub-flow invoked by the screen flow

In this setup, the subflow created above can be invoked via Apex  – by mimicking the inputs of the two Screen elements in Part 1). 

The call to the Flow can be put between the Test.startTest() and Test.stopTest() methods of a Test class, followed by asserting the output. 

 

 

 

 

Before Save Flows

Before Save flows, introduced recently, offer a faster alternative to record updates using Process Builder. The way to set up a flow as a ‘Before Save’ flow can be found by editing the ‘Start’ element of the flow:

 

 

 

 

 

 

 

I hope these pointers were useful! Let me know what you think of the content and please feel free to share other tips and tricks! You can directly contact me on kanav.khurana@nextview.nl. Stay safe!