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
SeanCenoSeanCeno 

Account Trigger Not Rolling Up To Parent Account

All,
I'm trying to sum the number of Contacts for Parent Accounts with certain checkboxes = true. We have a hiearchy of Accounts: Branch level and National Account level. The trigger is updating on the Branch level, but not on the National Account level (Parent Account). Do I need to write a separate trigger on Accounts to sum these values? Would that not cause a recursive trigger?

Class:
public class Contact_RollupProductInterest {
    protected final Contact[] contactNewList;
    protected final Contact[] contactOldList;
	Set<Id> acctIds = new Set<Id>();

    public Contact_RollupProductInterest(Contact[] contactOldList, Contact[] contactNewList) {
        this.contactNewList = contactNewList;
        this.contactOldList = contactOldList;
    }

    //Update Number of RXR Prospects
    public void executeNumberRXRProspects(){
    	for(Contact c : contactNewList){
    		if(c.AccountId != null){
    			acctIds.add(c.AccountId);
    		}
    	}

    List<Account> updAcc = new List<Account>();
    	for(AggregateResult ar: [Select AccountId acctId, count(id) ContactCount 
    						 	 From Contact
    						 	 Where AccountId in :acctIds AND NS_RXR_Prospect__c = true
    						 	 Group BY AccountId]){
    		Account a = new Account();
    		a.Id = (Id)ar.get('acctId');
    		a.Number_of_NS_RXR_Propects__c = Integer.ValueOf(ar.get('ContactCount'));
    		updAcc.Add(a);
    	}
    		if(updAcc.size()>0){
    	 		update updAcc;
    		}
    }
}

Trigger:
trigger MasterContactTrigger on Contact (
    before insert, after insert, 
    before update, after update, 
    before delete, after delete){
    Contact[] contactOldList = trigger.IsDelete ? null : trigger.old;
    Contact[] contactNewList = trigger.IsDelete ? trigger.old : trigger.new;
        
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
            // Call class logic here!
        }
        if (Trigger.isUpdate) {
            new CompletedEvents(contactOldList, contactNewList).executeTotalCompletedEvents();
            new CompletedEvents(contactOldList, contactNewList).executeTotalMeetings();
            new CompletedEvents(contactOldList, contactNewList).executeTotalConferenceCalls();
            new CompletedEvents(contactOldList, contactNewList).executeLastCompletedEvent();
            new CompletedEvents(contactOldList, contactNewList).executeFirstCompletedEvent();
            new CompletedEvents(contactOldList, contactNewList).executeLastCompletedMeeting();
            new CompletedEvents(contactOldList, contactNewList).executeFirstCompletedMeeting();
            new Contact_RollupProductInterest(contactOldList, contactNewList).executeNumberRXRProspects();
        }
        if (Trigger.isDelete) {
            // Call class logic here!
        }
    }
        
    if (Trigger.IsAfter) {
        if (Trigger.isInsert) {
            // Call class logic here!
        }
        if (Trigger.isUpdate) {
            // Call class logic here!
        }
        if (Trigger.isDelete) {
            // Call class logic here!
        }
    }
}

 
Best Answer chosen by SeanCeno
Shruti SShruti S
For your Parent Record, you will have to write a different Trigger. Because the Parent Record is not directly related to the Contacts.
User-added image

As you can see, the Parent Record is not directly connected to the Contacts associated with the Child Accounts. Hence you will have to write a seperate Trigger for the Parent Record.

Note: This might be a little tricky if you have a complex hierarchy in place.