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
S_BatmanS_Batman 

Adding custom field from Contacts and Showing on 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?
Best Answer chosen by S_Batman
NagendraNagendra (Salesforce Developers) 
Hi S_Batman,

Please check with the below thread from stack exchange community exactly with similar error which has a suggested workaround.

Modify your code as per the requirement which should help you to surpass the above challenge. Best Regards,
Nagendra.P

All Answers

NagendraNagendra (Salesforce Developers) 
Hi S_Batman,

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
                                                    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>();
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();
}
}
You can also 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 Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue and becomes a proper solution which results in helping others who are really in need of it.

Best Regards,
Nagendra.P



 
S_BatmanS_Batman

Nagendra:

Thank you but 

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

The bolded field, should that be removed or replaced with the Field_On_Account__c ?
 

NagendraNagendra (Salesforce Developers) 
Hi S_Batman,

Yes, give a try by replacing the above field which should suffice your requirement.

Thanks,
Nagendra.P
S_BatmanS_Batman
Nagendra,

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: ()
 
NagendraNagendra (Salesforce Developers) 
Hi S_Batman,

Please check with the below thread from stack exchange community exactly with similar error which has a suggested workaround.

Modify your code as per the requirement which should help you to surpass the above challenge. Best Regards,
Nagendra.P
This was selected as the best answer
S_BatmanS_Batman
Thanks Nagendra!