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

ContactSummaryFlow

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

ContactSummaryProcess

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.

ContactDeleteFlow

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

ContactDeletePage

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 ...
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 different organizations. ・Deploying metadata base on gi...
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 ...
Salesforce Smart Importer – 1-Drag to import... This is my third Chrome Extension named Salesforce Smart Importer (Other two popular extensions are Salesforce Mass Editor and Salesforce DevTools), it is designed for Salesforce not only admin but also normal user, supports : ・1-Drag to import any Excel / CSV data on anywhere in Salesforce, both ...