Blog

Loops in Visual Workflow

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.

Use Case

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

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.

Variables

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:

Now that the variables have been created and defined, the logic of the flow can be created.

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.

As you can see, in this action we iterate over the entire collection and use the Case variable as the loop variable. It’s time for adding additional actions to close and update cases! Now to check if the case is already closed, a Decision action is added:

In the next step we use an Assignment action, to update the Case Status and Case Reason fields to the constant values we defined. We also save the case contact e-mails to ContactEmail collection.

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:

Dear customer. Your Cases have been closed because related Account {!account.Name} is Inactive’
Now all required actions have been defined; however, there are a few remaining actions:

  • 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

Focus on Automation: Visual Workflow Loops

Loops in Visual Workflow 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… Read More

Focus on Automation: Visual Workflow Loops

Salesforce Manager Group Sharing Example

Manager Group sharing is a little tricky to understand until you see example scenarios. Here we go through a couple of examples to explain how it works. Understanding the Need for Manager Sharing Groups It is a common practice to group users sharing the same business duties under a single Role. For instance, if your organization has 15 territories within a country – and each territory has an Area Sales… Read More

Salesforce Manager Group Sharing Example

Focus on People: Valerio Lancia

I recently had a chat with Valerio Lancia, a Salesforce CRM Application manager about his journey with Salesforce, what he has learned and his recent certification experience. Q: What is your background? Is it an IT background, or a business background, and how did you first get exposed to Salesforce? A: I have a management engineering degree and my initial job in Italy was as a buyer. After two years… Read More

Focus on People: Valerio Lancia

An introduction to Salesforce APIs

Once you start working in the Salesforce world, it won’t take long until you come across the need to integrate or connect your Salesforce org to an internal or external system. At that point, you will start to hear about API’s. While you may think that is something that only developers need to know and understand it is useful for anyone working with Salesforce to know what they are and… Read More

An introduction to Salesforce APIs

Getting Started with Salesforce Entitlements

When a customer calls and requests support, how does a business know if and what level of service they are entitled to? Or what if service is only provided on certain products? Service could be provided for a certain period (e.g. a warranty) and then a customer must pay for service beyond the warranty period. It could also be that customers can purchase extended warranties or service contracts that may… Read More

Getting Started with Salesforce Entitlements

Winter 16 is Coming

Winter is Coming… While you are pondering over deploying the new Summer ’15 features, here is more news. The plan for Salesforce Winter ’16 release is out. It is time to plan your registrations for getting a sandbox preview of Winter ’16 features as and when it becomes available. You would need to take action before September 4, 2015 in case you are planning to make your Sandbox part of… Read More

Winter 16 is Coming

Native Duplicate Management in Salesforce

Administrators (and developers) are often faced with the task of cleaning up duplicate data from an org. Contact and Lead objects are the usual suspects for duplicate data. Anyone who has made an attempt at cleaning up duplicate data would agree it is a daunting and quite unenviable task. Duplicate data weighs down on your employee performance, application usability, risks customer satisfaction, and skews analytics. So, it is a priority… Read More

Native Duplicate Management in Salesforce

Salesforce Multi Org Dilemma

What exactly is a Salesforce org? The Salesforce document glossary describes an org (or Organization), as “A deployment of Salesforce with a defined set of licensed users. An organization is the virtual space provided to an individual customer of Salesforce. Your organization includes all of your data and applications, and is separate from all other organizations.” So, what is a multi-org situation? The multi-org situation that I refer to in… Read More

Salesforce Multi Org Dilemma

Salesforce Lightning Connect Example

Unlocking Back Office Data through Salesforce Lightning Connect The general approach to enterprise programming is changing. Significant investments are being made in making enterprise IT applications agile and nimble. Salesforce, being the innovators they are, have taken significant strides in this direction. One such move is the introduction of the lightning framework – a philosophy pioneered to reduce the time in developing and deploying enterprise applications. For any enterprise system… Read More

Salesforce Lightning Connect Example

Salesforce Process Builder Example

Lightning Process Builder – Point & Click Process Definition in the Cloud  The Spring ’15 release of Salesforce has opened up a whole new philosophy of developing in the cloud – the lightning framework. One key element of the lightning framework is the Lightning Process Builder. Before diving into Lightning Process Builder, it is good to understand where Salesforce is going with the entire “Lightning” idea. It is positioned as… Read More

Salesforce Process Builder Example

Salesforce Email to Case Setup Example

Email to Case is a standard Salesforce feature that allows cases to be created from emails.  We will be looking into the detail of On-Demand Email-To-Case and Email Services.On Demand Email-To-CaseThe first question when considering this functionality maybe which email address should be used to send to? We cannot send email to just any email address and have a Salesforce case created. We need to generate it using Salesforce configuration.Go… Read More

Salesforce VLOOKUP Example

Many people are aware of the Microsoft Excel vlookup function, that will return a value in a table (or defined range of cells) based on an identifier. Salesforce has a function with the same name, and this post will explain how it can be used using an example.Let’s say that you have a business requirement that only cases that have been open for a certain period of time can be… Read More

Salesforce VLOOKUP Example

Salesforce Multiple Record Types Example

Creating Multiple Record Types Creating brand new custom objects in Salesforce, while necessary, can be time consuming and tedious. Often times you will need to create a different page layout or new selections for a particular picklist field but the only way you know how to do this is create an entirely different custom object and customize it as needed. This is where creating multiple record types for one object… Read More

Salesforce Multiple Record Types Example

Creating a Dynamic Dashboard in Salesforce

Dashboards are a great way to represent data visually within an organization. They can show how you have performed in the past and what’s happening at the moment – two keys to any business that wants to be successful.Normally, dashboards in Salesforce are run based on one user for the entire organization. Meaning the dashboard looks the same for all users within the organization. This is great if you are… Read More

Creating a Dynamic Dashboard in Salesforce