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.
If an Account is no longer active, any open related Cases should be closed and an e-mail sent to the Case Contacts to notify them. On the Account Object there is an Active picklist field, with the values ‘Yes’ and ‘No’.
Here is an overview of how we will be using Visual Workflow and Process Builder:
- Use Process Builder to trigger the Flow on Account create or update.
- Define a Criteria to only process Accounts marked as Inactive.
- Use an Immediate Process Builder Action to call our Flow, passing the Account Id of the record updated.
- Search and store related Cases to the Account in the Flow.
- Loop through related Cases and update Case Status to closed, Case Reason and store Contact Email in a collection variable.
- Save changes to the Case Records (using Fast Update).
- Send e-mails to all Contact Emails (using the collection variable).
Our first step, after creating a new Flow, is to define the variables and constants required.
Constants are static values that can be defined once, and then used as future references later in the flow. The advantage of using constants is that if the value changes, then it only has to be changed once
ACCOUNT_ACTIVE_NO, INACTIVE_ACCOUNT_REASON, STATUS_CLOSED
Constants contain static values that are referred to later on. For example, they contain the Case Reason value used when updating the Cases.
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:
- Set the Get Account action as the Start element
- Reference the related actions. End of loop references the Update Cases action and return into the loop is connected to the Case already Closed decision.
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:
- Creating an account, a Contact and an open case.
- Saving the account.
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.
What Certification are you studying for now?
Focus on Force currently provides practice exams and study guides for sixteen certifications
Hi Lesile,
Great, thanks for letting us know you figured it out.
Martin
I figured out what caused the error. During Fast Lookup of the case object, instead of saving the contactId, I need to save the contactEmail.
Please help. I did this exercise. When I change the Account Active to “No” I get this error message: “The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 30141000000RWgc. Flow error messages: An unhandled fault has occurred in this flowAn unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help.” I went through the steps and make sure the screens matched. I couldn’t figure out why the error. You can see the more clear error message from: https://na35.salesforce.com/designer/designer.apexp#Id=30141000000RWgc
Thank you.