Create Roll-Up Summary field to count Contacts on Account with Process Builder and Flows

As we know,roll-up summary field is used to display a value in a master record based on the values of fields in a detail record. The detail record must be related to the master through a master-detail relationship.
Unfortunately Account and Contact objects only have a lookup relationship so we cannot create roll-up summary field on Account such as to count the number of contacts related to that account.

Writing a Trigger for Contact object is a general solution in this case,but there also have a no coding way to solve this problem, with Process Builder and Flows.

Here is the list of setting we have to create:
ContactSummaryFlow (Flows)
ContactSummaryProcess (Process Builder)
ContactDeleteFlow (Flows)
ContactDeletePage (VF Page)
※wow,doesn’t that look more complicated than a Trigger? well,maybe the other guys don’t think so, especially the clients. ; )

New custom field

Create a custom field(Number(18, 0)) on Account named “NumberofContacts”. NumberofContacts


Create a Visual Workflow from Create > Workflow & Approvals > Flows. ContactSummaryFlow Add a Decision element(Drag & Drop) named “ValidateAccountId”. f3_3 Create a variable named “varAccountId”. f3_2 varAccountId Add a Fast Loopup element named “SearchContacts” to flow area. SearchContacts Create a SObject Collection Variable named “contacts” for storing search results. f4_2 f4 Now the Fast Loopup “SearchContacts” should look like the following image. f5 Then add a Loop element named “LoopContacts”. LoopContacts Add a Assignment element for the Loop element “LoopContacts”. Assignment Assignment Add a Record Update element at the end. Record Update element Now,set the Decision element to be a Start Element and link the elements with mouse like the following image. f10 Save flow as “ContactSummaryFlow” and activate it. ContactSummaryFlow ContactSummaryFlow


Create a process named “ContactSummaryProcess” from Create > Workflow & Approvals > Process Builder. Process Builder 1 Add Contact object and save it. Process Builder 2 Add a criteria and save it. Process Builder 3 Add a action for the criteria when it is true,and select the flow “ContactSummaryFlow”. Process Builder 4 Now activate the process. Process Builder 5 Process Builder 6 So far we have completed setting for counting number of contacts when contact is created or updated, but the process cannot be started when object is deleted.
Then we should create a delete flow and a flow page next for fixing this issue.


Create another Visual Workflow named “ContactDeleteFlow”.

Add a Record Delete element “DeleteContact” and a variable “varTargetId”. Flow Designer  DeleteContact Flow Designer  varTargetId Add flow “ContactSummaryFlow” at the end. Flow Designer  varTargetAccountId Flow Designer  ContactSummaryFlow Flow Designer  ContactSummaryFlow Save and active it. Flow Designer  ContactDeleteFlow Flow Designer  ContactDeleteFlow


Create a Visualforce Page named “ContactDeletePage” with the following source code.

Replace Contact delete action

Replace the standard delete action of Contact with vf page “ContactDeletePage”, from Customize > Contacts > Buttons, Links, and Actions. Buttons  Links  and Actions  Contact   1 Override Standard Button or Link  Delete   2

Operation check

Create a contact

Result 1 Result 2

Delete contact

Result 3 Result 5 It looks like working well!

At the next post I will share you the other interesting usages of Process and Visual Flows.
Enjoy it!

Related Post

Save approver automatically in Approval Processes ... In previous post,I shared how to 《Create Roll-Up Summary field to count Contacts on Account with Process Builder and Flows》. In this post I will share about how to save approver(current user) to the target object automatically in Approval Processes, with Process Builder and Flows. Actually,this ...
Using Promise for Apex Server-Side request in Ligh... In Lightning Components, each communication with Apex Server-Side is handled as a Javascript async request, and the callback you set is called after the server-side action is completed. A server-side action can return any JSON object, such as sObject or a map of name-value pairs. The followoing s...
Pipeline for Salesforce – Be less busy, less... Overview Pipeline is a metadata deploy app for Salesforce, supports: ・Making Changeset with Excel file in seconds. ・Exporting Pipeline target metadata list to Excel file. ・No dependencies on Salesforce Org, deploying metadata between any different organizations. ・Deploying metadata base o...
Salesforce Mass Editor (Chrome Extension) – ... This's my second Chrome Extension (First one is HERE) this year, named "Salesforce Mass Editor" and it supports for doing the below things : ・Makes any Salesforce list view to be a powerful mass editor. ・Mass insert, mass clone, mass update, mass delete, ALL-IN-ONE. Support for both Classic and ...