I am having account hierarchy, i.e. there is one parent account and multiple child account. They are connected through Hierarchy relationship field which is lookup on the child account, i.e. on the child account there is lookup field for the account.

Example: ABC corp(parent account) & ABC Corp-US(child account)

Hierarchy is like:

-ABC Corp

  -ABC Corp-US

There is field called Amount on account object which is populated for all the child and parent records. I want the sum of that field and display the result on the Parent account. How can we achieve this?

I tried but since it is hierarchy relationship how to add up Amount field.

Since it is single object 'Account' and it is having same field Amount for both Parent and Child.

  • 3
    In the end, I don't think it should really be much different from rolling up any other field. A trigger would do the job, and the DLRS tool might be able to handle it as well. Maybe someone with more experience with DLRS would like to chime in. – Derek F Jan 14 '20 at 14:17
  • DLRS is definitely the off-the-shelf answer here. It behaves like any other lookup relationship save the built-in protection against reference cycles. There might be some additional complexity if it is a multi-level roll up but I only see parent and child here. – David Reed Jan 14 '20 at 14:18
  • The main problem is identifying all of the Accounts in the hierarchy and the top parent Account. Starting with any account, you need to look for the Parent.Parent... 5 levels with a SOQL Query and repeat if you don't hit the top Account. Then you need to reverse and find all of the children in the tree and this can take another set of SOQL Queries going the other direction. After you identify the top Account and the connected Children you can roll up whatever Amount field etc. – Bill Appleton Jan 14 '20 at 14:47
  • @BillAppleton : thank you soo much for your input. Do you have some sample code to achieve this?? can we do this using Rollup helper?? – situ Jan 17 '20 at 8:25

The problem is finding the top Account first and then all connected Accounts given an Account ID. The answer is not pretty. To find the top Account, the most aggressive SOQL query is like this:

from Account where Id='0014000000phijeAAA'

You will have to loop on this query until you find the connected Account with no Parent ID. Now you have the object that will receive the sum.

Next, you have to go the other direction and identify all of the connected Accounts. The most aggressive SOQL query is like this:

ParentId='0014000000phijeAAA' or
Parent.ParentId='0014000000phijeAAA' or
Parent.Parent.ParentId='0014000000phijeAAA' or
Parent.Parent.Parent.ParentId='0014000000phijeAAA' or
Parent.Parent.Parent.Parent.ParentId='0014000000phijeAAA' or

This will retrieve all child Accounts 6 levels deep. For any account that hits the final level you need to recurse on the query because there might be even more children connected to that account.

After that the rollup is pretty simple, just sum all the children Amounts and set a custom field on the top parent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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