Since the Roll-up Summary option does not work from Contacts to Accounts..

I have a custom number field on the Contact level (let's call it ABC), and I want to create a Field on the Account to show the total of all the ABC fields from all related Contacts to an Account. Can this be done without triggers?

up vote 1 down vote accepted

I have modified the above trigger you mentioned to update the Account field with SUM of all its related contact Custom Field. Replace the Field_On_Contact__c with Contact Field API name and Field_On_Account__c with Account Field API name.

trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, after update) {

Contact[] cons;
if (Trigger.isDelete) 
    cons = Trigger.old;
else
    cons = Trigger.new;

Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
   acctIds.add(con.AccountId);
}

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([SELECT Id ,AccountId, Field_On_Contact__c FROM Contact WHERE AccountId IN :acctIds]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([SELECT Id, Field_On_Account__c FROM Account WHERE Id IN :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<Id> conIds = new Set<Id>();
Decimal totalValue = 0;
for (Contact con : contactsForAccounts.values()) {
    if (con.AccountId == acct.Id && con.Field_On_Contact__c != NULL) {
        totalValue += con.Field_On_Contact__c; 
    }
}
acc.Field_On_Account__c = totalValue;
}
if(acctsToUpdate.values().size() > 0) {
    update acctsToUpdate.values();
}
}

Hope this will help You.

  • Thanks AKSHAY; however should the Number_of_Contacts__c be replaced with Field_On_Account__c ? – B.Wayne Oct 19 '16 at 13:08
  • Yes. Sorry that was my mistake. In query replace that. – AKSHAY Dhiman Oct 19 '16 at 13:10
  • I replaced it, however now I am getting an error Compile Error: unexpected token: where at line 19 column 77 – B.Wayne Oct 19 '16 at 16:51
  • Thank you I was able to replace it and save it. However, when I update the contact field to test it I am getting the following error: Error:Apex trigger ContactSumTrigger caused an unexpected exception, contact your administrator: ContactSumTrigger: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Number__c: () – B.Wayne Oct 19 '16 at 16:57
  • Try it again. I have made the change. – AKSHAY Dhiman Oct 19 '16 at 17:21

You can use Rollup Helper Free Edition - A Helper Suite App which provides following functionalities:

Rollup Helper rolls-up any Salesforce data. Supports count, sum, max, min, average, percent, lookup rollups, text, and multi-currency.

And its free.

  • With the Rollup Helper Free edition - you have to trigger it each time and you only get 3 "tries". I would require something that's more ongoing. – B.Wayne Oct 18 '16 at 19:20
  • Then you can simply write a trigger – Santanu Boral Oct 18 '16 at 19:29

Are you able to help me with the trigger? I was able to find the Trigger to count number of Contacts:

trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, 

after update) {

Contact[] cons;
if (Trigger.isDelete) 
    cons = Trigger.old;
else
    cons = Trigger.new;

// get list of accounts
Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
        acctIds.add(con.AccountId);
}

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
                                                        ,AccountId
                                                        from Contact
                                                        where AccountId in :acctIds]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
                                                             ,Number_of_Contacts__c
                                                              from Account
                                                              where Id in :acctIds]);

for (Account acct : acctsToUpdate.values()) {
    Set<ID> conIds = new Set<ID>();
    for (Contact con : contactsForAccounts.values()) {
        if (con.AccountId == acct.Id)
            conIds.add(con.Id);
    }
    if (acct.Number_of_Contacts__c != conIds.size())
        acct.Number_of_Contacts__c = conIds.size();
}

update acctsToUpdate.values();

}

Can I update this to just count the custom field within the Contact?

If you want to do this without writing your own trigger, and don't want to use Rollup Helper Free Edition, you could instead consider Andrew Fawcett's Declarative Lookup Rollup Summaries app, which is 100% free and can accomplish your goal.

It (just like Rollup Helper), DOES use triggers, but it allows you to create these rollups without advanced trigger knowledge through a pretty simple to use UI within Salesforce

https://github.com/afawcett/declarative-lookup-rollup-summaries

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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