I wish to write a trigger for to sum up Fees (Custom Field) on the Contact object. The sum needs to be displayed on the account object as a Total Fee.

For Example - I have an Account A1 that has 3 contacts(C1,C2,C3) where the fees are 500,400,400. I wish to show the sum 1300 on the custom field (Total Fee) of the Account object.

Please help me, I have made triggers for adding total fees under account, but I need a little bit of guidance.

  • Is this not doable through a formula field on the account? – abhi Apr 15 '15 at 15:04
  • 3
    what is the issue that you are facing? can you please share the trigger that you have written? – Rajiv Bhatt Apr 15 '15 at 15:17
  • 2
    Share your existing code so we can help you. – Richard N Apr 15 '15 at 15:17
  • I am mistaken. The contact object does not roll up to account. – abhi Apr 15 '15 at 15:32

Account does not allow you to write rollup summary field to rollup values from Contact. So your decision to go for trigger is correct. It can be done in two ways in trigger.

1) Using aggregate query in trigger - First you need to get related account Ids when contact is updated. Then query contact aggregating the fee field grouped by account. Then iterate over aggregate result and save the aggregated value to account

2) Using child query in trigger - Get related account Ids. Query account with contact as child query. Iterate over the result in a for loop and sum the fee field values and save it to account.

  • Finally worked..thanx for help Victor..i made the code and its working..but hey i am unable to post here that code..because code length is exceeding..Can anyone tell me how to paste a screenshot here – Pradeep Kumar Apr 15 '15 at 19:14

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