{"id":146,"date":"2013-06-06T18:21:44","date_gmt":"2013-06-06T12:51:44","guid":{"rendered":"https:\/\/www.dhruvsoft.com\/blog\/?p=146"},"modified":"2024-07-26T09:49:43","modified_gmt":"2024-07-26T09:49:43","slug":"displaying-total-number-of-contacts-for-each-account-in-salesforce","status":"publish","type":"post","link":"https:\/\/www.dhruvsoft.com\/blog\/displaying-total-number-of-contacts-for-each-account-in-salesforce\/","title":{"rendered":"Displaying Total Number of Contacts for each Account in Salesforce"},"content":{"rendered":"<p>If you want to\u00c2\u00a0display the &#8220;<strong style=\"font-weight: normal;\">Total Number of Contacts&#8221; for each &#8220;Account&#8221;<\/strong> in Salesforce on the Account page you can not use the Roll Up Summary field. The Roll Up Summary fields on contact are not supported on Accounts. We have used a Trigger on the contact object to \u00c2\u00a0find the sum of the number of contacts which are there in an Account.<!--more--><\/p>\n<p>The trigger basically updates the Account custom field &#8220;Number_of_contacts__c&#8221; with the number of contacts that Account has.\u00c2\u00a0Since this trigger is on the Standard object it can be used in any Salesforce.com ORG by creating this custom field on Account.<\/p>\n<p><em>The code below is developed based on Design pattern &#8211; &#8216;Factory Pattern&#8217;. For more information about this design pattern please refer this <a href=\"https:\/\/www.tutorialspoint.com\/design_pattern\/factory_pattern.htm\">link<\/a>.<\/em><\/p>\n<p><span style=\"color: #ff0000;\"><strong>This code has been modified recently<\/strong><\/span><\/p>\n<p><script src=\"https:\/\/gist.github.com\/dhruv-soft\/82ab41d78a897183bd5f44787d66e8a7.js\"><\/script><\/p>\n<p>It works on delete of a contact and it also works even if you change the Account of a contact where it updates both the Accounts with the number of contacts.<\/p>\n<p><em>Note: This code has been modified recently &#8211; please share your feedback. The previous code which had several issues is still hosted <a href=\"https:\/\/gist.github.com\/dhruv-soft\/5721224\">here<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to\u00c2\u00a0display the &#8220;Total Number of Contacts&#8221; for each &#8220;Account&#8221; in Salesforce on the Account page you can not use the Roll Up Summary field. The Roll Up Summary fields on contact are not supported on Accounts. We have used a Trigger on the contact object to \u00c2\u00a0find the sum of the number [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[79,78],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-apex","tag-apex-trigger","tag-salesforce-com-apex-trigger","entry"],"_links":{"self":[{"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":0,"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dhruvsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}