function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Charni WigginsCharni Wiggins 

Trigger count related records (via lookup) on Contact to update Account

I am trying to write a trigger on Account that is essentially a rollup of related records on the Contact level. Not the most confident with apex so just looking for some guidance.. 

I have created it from the Event Reg object and have a for soql loop to get all Event Reg records, including the Contact and Contacts AccountId, but am stuck at what to do now. 

Any help would be much appreciated! 

Biswojeet Ray 11Biswojeet Ray 11

Hi Chami,

I have updated the total number of contacts in "Total_Contact__c" field of account. U can replace your appropriate field there(in Line no 19).

 

trigger accountTrigger in Account(Before Update) {
    set<Id> accountIds = new set<Id>();
    Map<Id, Account> accountMap = new Map<Id, Account>();
    for (Account acc : Trigger.New) {
        accountIds.add(acc.Id);
        accountMap.put(acc.Id, acc);
    }
    List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId IN: accountIds];
    List<Account> toupdate = new List<Account>();
    for (Id ids : accountIds) {
        for (Contact Con : contacts) {
        integer count = 0;
            if (Con.AccountId == ids) {
                count++;
            }
        }
        //Here you can put the field name on which you need to update total number of contacts
        accountMap.get(ids).Total_Contact__c = count,
        toupdate.add(accountMap.get(ids));
    }
    Update toupdate;
}


I hope it helps you.

Kindly let me know if it helps you and please mark as Best Answer.

Thanks and Regards,
Biswojeet
Ajay K DubediAjay K Dubedi
Hi Charni,
You can go with this code which is with best practice and optimised So try it. Here 'Number_of_Contacts__c' is a custom field in Account object of number type which contains the total number of related contacts .
//Trigger
trigger CountContactTrigger in Account(Before Update) {
    if(Trigger.isBefore && Trigger.isUpdate){
        CountContactClass.CountContactClass_method(Trigger.New);
    }    
}
//Helper class
public class CountContactClass{
    public static void CountContactClass_method(List<Account> accountList){
        set<Id> accountIds = new set<Id>();
        Map<Id, Integer> accountMap = new Map<Id, Integer>();
        for (Account acc : accountList) {
            accountIds.add(acc.Id);
            accountMap.put(acc.Id, 0);
        }
        if(accountIds.size() > 0){
            List<Contact> contactList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN: accountIds LIMIT 10000];
            for (Contact Con : contactList) {
                if (accountMap.containsKey(Con.AccountId)){
                     accountMap.put(Con.AccountId,accountMap.get(Con.AccountId)+1);
                }
            }
        }
        for(Account accObj : accountList){
            accObj.Number_of_Contacts__c = accountMap.get(accObj.Id);
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Charni WigginsCharni Wiggins
Hey both, thank you for your suggestions... i think I need to write it from the custom child object (event_reg), so when these are update/created/deleted, it updates the account. Could I follow a similar pattern to your suggestions still?