Create a Roll-Up Summary with Flow
Here is a (relatively) simple example of how to create a roll-up summary that counts how many contacts that exist for a given account and keeps this number up to date as you add/edit/delete contacts that are related to this account.
You can easily take this principle and apply it to any two objects.
Create a new Account field
Starting with the Account object (where we want our counter) we create a new number field (with 0 decimal places) called Count of Contacts (Count_of_Contacts__c).
Now we are ready to create our first flow:
To start building this flow, create a new flow, type = Record Triggered Flow.
The object is the contact as we want to see when a change occurs on a contact record – eg it gets related to an account. We need to do this after the contact record is saved.
Unfortunately there are no filters on the contact that we can use to determine if we need to update the account contact count so we have to check them all. It’s not a very efficient method but it only runs when we create or update a contact record. The ideal situation would be to only update those where the record is new or the AccountId has been added or changed. Using the ISCHANGED operator sounds like the solution, however it will always be true when we create a new contact as the AccountId will go from null to a valid AccountId.
Add a Decision element:
Now we can add a decision to see if the AccountId has been changed. The YES condition is for an existing contact record where the related Account (Account ID) has been changed.
Add a Get Records element (for Prior accounts):
Now we are heading down the Yes side of the branch:
Here we want to get any contact records where the Account Id was changed on the contact. We need to update this account because the contact record may no longer be related to the account and the count needs to be recalculated.
We want to count any contact records where the Account Id is the same as the contact record that triggered this flow – prior to the change (they were related to the same account). To do this, we use the $Record_Prior.
We get all the related records but only store the Id (of the contact) and the Account Id fields. We store these into a variable rc_ContactsPrior which is a record collection of contacts, potentially containing many contact records.
First, we need to create a variable to save these records to:
(see below to create rc_Contacts).
Create this new variable:
Back to the get records element:
Create another variable to store how many contacts were found:
Create an Assignment Element
Set our new counter to the number of contact records we found for the Prior record..
Add an Update Records Element:
Next, update the Prior Account record with the new count of contacts:
Now we do the remainder (for all records) so we cover records where a new contact has been added, this will update the count for them.
Get our contacts, this time we are using $Record for the current Account record.
Add a new record collection variable to count them:
Add the fields we want in our record collection:
Create another new variable for the count of records:
Assign the value to the counter: (add Assignment)
Update our Account with the new counter value:
Save your Flow
Finally check your flow looks like the one in this pic:
and Save your flow giving it a meaningful name eg:
Enable your flow.
Now we have a counter for when a contact is updated and linked to our account. But what happens if the contact is deleted? We need another flow!
It is simpler than the previous one.
Create another Record Triggered Flow.
This one will take care of any contact records that are deleted.
Again, create a new Record Triggered Flow.
This time the trigger is when a record is deleted and we can run it before the record is deleted.
Create the rc_Contacts collection variable
Get all contacts where the Account ID is the same as the record being deleted
Create the counter variable
Add the Assignment to both count the number of matching contacts AND to subtract 1 for the record being deleted.
Update the Account with the new count of contacts.
Save the Flow eg as: Accounts-CountContacts-DeleteTrigger
Activate the Flow.
Add the Count of Contacts to your Account Page Layout.
Now, you can test it out. Find an account that already has a related contact and add another one.
Change the Account for an existing contact and check that both the old account and new account now have the correct counters. Note: You may need to refresh the account page to get the new values.
Want more great flow learning like this?
Head over to our new Learning Salesforce Flow course at CertifyCRM.com to get started on your flow journey.