1

I am facing issue in getting number of contacts for an account in report. There are triggers available but that can work for 1000 records at a time. I have been seeing lot of blogs in identifying the count of contacts in account but all development was 2 -3 years back that is to triggers. Is there any new way hat we can use to catch number of contacts in account? Any help would be very useful..

  • Are you looking to get the number of contacts onto a field on the account object? – battery.cord Sep 9 '16 at 17:57
  • Yes ,I am looking for something like that and use that variable in custom reports to filter out data. – sinner Sep 9 '16 at 18:12
1

You do not need to make a field to do this. Use the report type "Contacts and Accounts", and then use a summary report and sum by contacts. See my image below. Let me know if this fits your need, if not elaborate more please.

enter image description here

  • I already did sorting on account name but there is an issue ,I am using HEDA version of salesforce and in that architecture there are multiple affiliated type for each contact therefore even if you have one contact even then it will show multiple records because multiple affiliations are attached to it. I was thinking to get field value of contact count so that it will be easy to identify duplicate records. – sinner Sep 9 '16 at 18:39
  • @sinner ah shoot, yeah thats a bit different. I would look at some apex for this. – All about the W Sep 9 '16 at 19:04
1

First, if you haven't worked with triggers or apex code before, I'd highly recommend going through the Apex Trigger Trailhead before you try implementing the solution below.

Creating a trigger will ensure any new records have the proper values, and that any changes to existing contact lists will be reflected.

trigger ContactCounter on Account (before update) {

    List<Id> accountIds = new List<Id>();

    for (Account a:Trigger.New) {
        accountids.add(a.Id); // Move Trigger Id's into list to use in query 
    }

    // Get a list of Accounts, with related contacts. Minimal field data. 
    List<Account> accountsWithContacts = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIds];

    for (Account a:accountsWithContacts) {
        // Access the data in the trigger via newMap.get()
        // Assign a field a value based on the size of the list of contacts on the account, which is returned from the query 
        Trigger.newMap.get(a.Id).Contact_Count__c = a.Contacts.size();
    }

}

You'll also need to run an update in your developer console, via Anonymous Apex. This will ensure your existing Accounts have the correct data. Just doing something like the below should work, once you run it a few times:

// Get a list of Accounts, with related contacts. Minimal field data. 
List<Account> accountsWithContacts = [SELECT Id, (SELECT Id FROM Contacts) FROM Account LIMIT 500];

for (Account a:accountsWithContacts) {
    // Access the data in the trigger via newMap.get()
    // Assign a field a value based on the size of the list of contacts on the account, which is returned from the query 
    a.Contact_Count__c = a.Contacts.size();
}

update accountsWithContacts;
  • I am trying to update count value with old account using anonymous Apex but it is not updating value for every account ,I tried running it couple of times but still not able to update that. – sinner Sep 9 '16 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.