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
Vijay NaikVijay Naik 

Roll-up Values in case of Self-Lookups

We have a very interesting and a challenging requirement from one of our clients, following is the scenario:

Consider there is a customer Cust-A.
Cust-A can refer one or more customers. Say, Cust-A refers Cust-B and Cust-C.
Further Cust-B and Cust-C can refer one or more customers.
This chain of referal can go upto any level.

Now, on each customer record we want to calculate total number of refered customers in the entire chain below that customer.

Has anyone come accross such a scenario or have any suggestion on how this can be implemented.

All suggestions are welcome and appriciated.

Thanks,
Vijay
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Vijay Naik:

 Im afraid that there is no standard roll up way to handle this and more over it looks like trigger is the only option available ( as the condition depth is multi level). To be in detail, you need a trigger on object at which you were capturing the refferal information, which maintains the count on the reffered by customer records.

Consider Customer A refferred Customer B.
> On insert of a refferal record with A & B combination,  
    -Query for all the refferal records who reffered Customer A.
    - Update the counts of all reffered customers including A by +1.
> On update (reparenting) of a refferal record,
    -Query for all records with old customer value in the reffered by lookup and update all those records including the old reffered bycustomer value           record by -1
    -Query for all records withnew customer value in the reffered by lookup and update all those records including the new reffered by customer            value record by +1
> On  delete , query for all reocords with customer A as refferal record and update count of all those customers by -1.


Hope this helps:

Thanks,
Balaji Garapati
Vijay NaikVijay Naik
Thanks Balaji for your response.

I think the trigger will not work here because of the self lookup, the trigger will fail after 16 iterations and I guess it throws Maximum Trigger Depth Exceeded error.

I think we need to use some kind of data structure or a design pattern to avoid any kind of governor limits.

Please let me know if someone has come accross such a requirement or has some ideas around this requirement.

Regards,
Vijay
 
Chris AldridgeChris Aldridge
Hi Vijay,

Did you ever find a solution to this?

I'm having a similar problem - account hiearchy basically, but the hierarchy can potentially be hundreds of records deep.

Wondering if you found a good pattern for this?  A trigger will not cut it for large data structures.
Kamal Kishore SinghKamal Kishore Singh
Hi

I am also facing a similar problem.I want to calculate a field(sum of all childs) in this look up.If any body has a solution do let me know.

Regards
kamal
Chinmay GolegaonkarChinmay Golegaonkar
Hi 

Anyone get the answer for this? I am also facing the same issue.