Visual Workflow enables many possibilities to automate processes and reduce or eliminate the need for Apex code. One of the key features of Visual Workflow is the ability to process multiple records using a loop construct, unlike standard workflow, where actions such as a field update operate on only one record.
Although Process Builder can operate on multiple records to update related records; once the logic becomes more advanced, Visual Workflow is the more appropriate tool because it includes conditional logic, branching, and a variety of actions that can be performed in conjunction with variables and loops.
Our first step, after creating a new Flow, is to define the variables and constants required.
accountId – This is a Text variable which contains the Account Id of the record being processed. It will be initialized from outside the flow, so it has ‘Input only’ as the value for the visibility parameter:
account– sObject variable to store Account instance:
cases – sObject collection variable to store related Case records:
case – sObject variable used in loop:
closedCases – a sObject collection variable to store updated Case records:
contactEmails – a collection variable to store Case Contact e-mails. Used in e-mail sending:
To begin, the Fast Lookup action will be used to retrieve the Account record related to the accountId variable. Fast Lookup allows an entire object to be stored in an sObject variable.
Now we need to get all related Case records. So, let’s add another Fast Lookup action:
Now we have all of the related cases, we need to loop through them. A Loop action is used which will iterate through them and store the current case object in a variable.
Next, another Assignment action is used to add the case records that were closed to a collection.
So now, we need to commit changes to all open cases have been updated. The next action will be Fast Update. *Note: We cannot use a Record Update action as it doesn’t support a bulk update.
A final step remains – Notify the contacts of cases that been closed. This is done with a Send Email static action:
Here is the e-mail body text:
That’s the end for of designing the flow. Here you can see the result:
The final task is to create and assign a process to our flow.
The Account object is added and the ‘When a record is created or edited’ condition:
In the Criteria area we need to check if the account has been updated to Inactive.
Now the flow is added as an action to the process. The ‘Flow’ action type is selected, and the accountId is passed as a flow variable.
After activating the Process, it can be tested by:
As a result you can see that our test case is closed with ‘Inactive Account’ reason and an e-mail notification has been sent:
So that’s it! This used to require APEX code and test coverage, but we have managed to use Visual Workflow instead. It will also work in Bulk, as there are no database updates inside loops.