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 Flow? (Don’t forget Enterprise Edition is minimum org requirement here!)
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.