You are hit with some tough demands from someone inside your organization. They want to be able to click on an Account and see the dollar amount of widgets their clients have sold, and be able to see that value on each level of the hierarchy. Well, this throws Roll-Up Summary fields out the window! Unfortunately they do not want to use a report to do this, so we need to create an Autolaunched Flow to calculate this for them.
Keep in mind
- Flows are triggered by Process Builder and that limits them from triggering BEFORE they are deleted. So, if your company is deleting Accounts and Opportunities often, then you might need to build a recalculate button (coming soon!).
- Standard Roll-Up Summary fields do not go up and down the hierarchy. They only work for one record.
- Standard Roll-Up Summary fields are formula based, and do not allow us to do Record Updates. You could use one in the Flow, but I prefer to use as few fields as possible and will create a custom Currency Field to be the value we use.
- We will be creating TWO Flows and TWO Process Builders (to trigger our Flows). The main purpose of this is that we can accomplish the hierarchy Roll-Ups with a smaller Flow and have it work for any number of parent-child relationships.
Flow #1 Breakdown
We are going to create a Flow that will find all Won Opportunities associated to an Account. Every time an Opportunity is Won, with the Opportunity Type of Chicken, we want to update the custom Roll-Up Summary field on the Account.
Lets get Started!
On the Account create a Currency Field with 2 decimals called Amount of Chicken Sold (Setup | Customize | Accounts | Fields).
Create a New Flow. (Setup | Create | Workflows & Approvals | Flows)
Lets start with the variables we need to create for this Flow.
Two Text Variables, called OpportunityId and AccountId
One Currency Variable, called Opportunity_Amount
Two SObject Variables, called Collected_Opportunity and Collected_Child_Account
Two SObject Collection Variables, called Opportunity_Collection and Child_Accounts_Collection
Does your Explorer tab look like this?
If yes, lets start to build out the Flow!
We will start with a Record Lookup. We want to grab the Account Id using the Opportunity.
Add a Fast Lookup element so that we can create a collection of all the Opportunities associated to this Account, with our filter criteria (IsWon = True and Type = Chicken).
Create a Loop element for us to go through the collection of Opportunities and add the values together.
Create an Assignment element to add up the value of every Opportunity this Account has that meets our filter criteria.
Lets connect the elements we have created now. Assign the Assignment element to the Loop and set the first Record Lookup as the Starting element
Now that we have our first set of loops set. We need to query for the possibility of any Child Accounts that might have Opportunities with Chicken being sold.
Create a Loop element for us to go through the collection of Accounts
Now we need to do add every Child Account’s Chicken Amount to the Opportunity_Amount variable. This will allow us to get the total value of all the Opportunities associated to this Account and the Child Accounts.
Only one more element to go for this Flow! We need to create a Record Update to put the correct Amount of Chicken Sold on the Account.
To finish this Flow off, we now need to connect the elements we just built and save the Autolaunched Flow.
To fire off an Autolaunched Flow, we must create a Process Builder. We want this Process Builder to fire off the Opportunities so that we can have it update the Roll-Up Summary field anytime a change happens.
We want every Opportunity to pass through this Flow, because we have our filter on the Fast Lookup.
Now activate the Process Builder. Congrats on finishing Part 1 of the Roll-Up Summary! Part 2 will allow us to make sure all levels of the hierarchy are updated when an Amount of Chicken Sold value changes.