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!