Roll Up Summary fields are one of the most useful custom field types in Salesforce, but not when your object model is based on lookup relationships. You see this magic only works on objects which are related as master-detail.
So what are your options here?
Install an app such as Rollup Helper from the AppExchange or Andy’s awesome Declarative Rollup Lookups which both provide custom roll up functionality. Or of course, learn Apex and build your own… Nah! So how about just using the tools you have already in your org, namely Process Builder and FlowI (Don’t forget Enterprise Edition is minimum org requirement here!)
Use Case
Let’s go with a very simple object model – Time Entry with lookup to Project (feel free to use one of your own here). Now we want to summarise the Time Entries up to the Project level and populate the Total Time field.
Step 1 – Lookup all Time Entry records related to a specific Project record
Start a new flow and drag a Fast Lookup onto the blank canvas.
Give your Fast Lookup a name plus unique name (this is the element’s API name).
In the Filters and Assignments section, we add our child object name. This is the object with the records we want to roll up. Next in the field box enter the name of your lookup field. Using our example this will be Project__c object.
In order to lookup all related Time Entry records for the Project will need to define a Variable which will be passed in by Process Builder which we will build after the flow.
Give the Variable a unique name. Input/Output Type needs to be Input and Output so it will be accessible to Process Builder later on.
Staying with variables, we need an sObject Collection Variable for storing the results of our Fast Lookup:
Now we have everything required to complete our Fast Lookup element.
Add your sObject Collection Variable in the main variable field leaving us to specify the fields we want to store as part of this collection.
Time__c – This is the time entered by the user
Id – This is the salesforce Id of the Time Entry
Project__c – This is the salesforce Id of the Project (and also our lookup field)
Technically we don’t need the last two, but useful if we have to debug things…
Step 2. Loop through the records to calculate the Total Time for the Project
Few more Variables to go. Next we need to create SObjectVariable for storing a single Time Entry record as we loop over the Fast Lookup results.
You should be familiar now with creating Variables and some of their types.
Back to the flow canvas and drag a Loop element onto it.
Setting up the Loop takes our two previously created sObject variables; starting with the sObject Collection which contains the records we want to loop over using the sObject Variable, and storing each iteration of the Loop in the variable. Developers do this all the time, so you are definitely programming now.
Last Variable; this time we need to store the Total Time which is the Sum of all the individual Time Entry records.
Make this variable a Number variable completing the rest of the fields as shown.
Step 3 – Assign Time to Total Time for each iteration of the Loop
Ok, next we need drag an Assignment element onto the canvas. Assignment allows us to populate the Total Time variable as we Loop over the Time Entry records.
In the Assignments section, select our Total Time variable in the variable field. Next use the add operator as we want to add each Time entry time value to the total as we Loop the records.
Don’t worry that your Flow is not linked up yet, coming back to that one. Last flow element we need is Record Update to make the final update to our Project record with the Total Time value.
Here we use our Project Id variable to tell the flow which record to update and for the fields we select our Total Time field and map this to the Total Time variable.
Now we have all our flow elements in place we need to link them up.
Starting Flow element is the Fast Lookup element, make sure you hit the tiny green icon to indicate that the flow is the starting element. You will be warned if you forget this one.
Step 4 – Linking up the elements
First link Fast Lookup to Loop – drag from the diamond down to the loop
Next link Loop to the Assignment element
You will be prompted to specify the Loop Routing for the loop, we want to process all Time Entry records so accepting the default works.
This will create the link from the Loop to Assignment, we need to come back from the Assignment to the Loop. (i.e. creating the full loop).
Drag from the diamond back to the Loop and if done correctly should look like the above.
Last link in the flow is from the Loop to the Record Update which ends the Loop.
That’s our Flow done, next we head to the new Process Builder and create a dispatch process to run our Flow.
Step 5 – Now for Process Builder
Process Builder can be enabled on request or goes GA with Spring 15 (not long to wait).
Next create a new Process and pick the Time Entry object.
We want it to run when Time Entry records are created or edited.
First decision box we simply want to say No criteria—just execute the actions!
Now we need an immediate action to launch our flow:
Give the action a name and type the name of flow in the next field to find your flow; make sure you flow is Active first otherwise it will not appear. In the flow variable field type the name of Project Id variable; if it doesn’t appear then you have not made the variable an input variable in the flow. Set the value by clicking the pencil to toggle the selector to locate the Project__c field.
You can test the process now by activating the Process and Flow (which should already be active) – Worked?
Okay have you spotted the deliberate flaw in this solution? What’s the one gotcha with this approach? Processes cannot be triggered for deletes right, only create or edits, so how do we trigger a reCalc on delete of Time Entries?
Stay tuned, we will follow this up very shortly with one solution for this which takes us deeper into our Flow adventures.
Hi – A lot of the pictures aren’t loading.
Hi Alex – thanks for the comment. We’re aware of this issue and plan to fix it very soon. Please bear with us, and thanks for reading!
I hope the broken images are fixed soon 🙂
Us too! 🙂 …It’s on the list, we promise.
Should be fixed now. Sorry about the delay
-Mike
For anyone struggling with missing images in this post, we still have this on our list. But now, you can check out this new video post where Mike walks you through in step-by-step: http://www.salesforceweek.ly/2018/02/how-to-create-roll-up-summaries-using-flow-video.html …Enjoy!
hi how do i add condition that is i need only “open” status of Time entry? please clarify
Hello Latha,
Try adding additional filter like shown in link
https://cl.ly/022K450d0h2p
Give that a go and let us know if still stuck
-Mike
I implemented the same. Its working absolutely fine when we are updating or creating the childs.
But the parent is not updating immediately when the child is being deleted.
How to overcome this?
Yes…This would be helpful !!!
Try the guidance in Part 2 of this post: http://www.salesforceweek.ly/2015/02/how-to-create-roll-up-summaries-using-flow-part-2.html.
But, spoiler alert, you need to replace how a delete is requested in the UI (use a flow rather than the standard action).
I want to implement this doing a count of the related records versus sum of a field value on related records but am struggling at the assignment part where I only have the option of equals, add or subtract. Is it possible to do a simple count of records, if so, how do I do that?
Yes, it’s possible, and you’d do it in a very similar way. You would collect all the records you wish to count, then loop through them. For each record in the loop, add 1 to your starting value of 0. When the loop ends, your value will be set to the number of records in the set. It’s not as intuitive as a simple COUNT function, but it will get you the same result.
Great content! It helped me a lot.
Thank you!
Hello, I created such flow between Service Contract and Case. My Goal is to roll up the time worked on a specific Cases to the Service Contract, so that I see the total time worked on all cases on the service contract. This works just fine,
But is there a possibility to set the field value on the case back to zero without losing the aggregated value on the service contract?
Hi. I’ve created a new flow, but I’m not seeing the Fast Lookup element listed in Flow Builder. Did I miss a step or has the flow builder been updated? I know there were some updates in Spring 19 release. Are there any other configurable workarounds for
https://help.salesforce.com/articleView?id=flow_ref_elements_data_get.htm&type=5
That could help you. Fast Lookup element has been moved to Get Records Element.