You need to sign in to do that
Don't have an account?
Count Number of Parent Accounts
I'm writing a trigger that updates two fields in a custom object leverages a Related Account lookup field to populate two other lookups for the Parent account and Ultimate Parent account. The Related Account Id already exists in the data fed by survey information received through ClickTools, but the trigger's job is to populate Parent account and Ultimate Parent account. The point is, I need to populate Parent account with the first parent it finds and then the Ultimate parent at the top of the list. It seems to me that the only way I can effectively do this is by counting the number of parent accounts up the hierarchy from the account receiving the data, so here is my issue:
I've seen a number of examples for counting child accounts, but what I'm wanting to do is count the number of accounts above an account all the way to the top of the tree. Here's an example:
- Account 1
- Account 2
- Account 3
- Account 4
- Account 5
- Account 7
- Account 6
- Account 3
- Account 2
I want to know that, for Account 7, there are three parents above it. I also want to obtain the ID and Type from each Parent above it.
This being said, I want to be able to do this in a variable situation, whether it's 2 or 10 layers deep. Any ideas?
As I believe it is only fair to provide my full solution now that I have benefitted from input, here is the full code for the trigger I just completed. Also, even though this post will be marked as the solution post, I will make sure that mcrosby gets the Kudos. Thanks again!
All Answers
Here's something that you could try:
Using the returned list, you should be able to determine the count of the hierarchy and get the Id, Type for each.
Since you want to do this in a trigger, I'm not sure if you would run into any limits with this or not, so obviously some indepth testing would be needed. Worth a shot.
My brain must have been fried yesterday. It just goes to show you that asking someone else helps put a fresh perspective on things.
Anyway, the code you provided needed just a couple of tweaks to make it work. ParentId was ParentID in the 'if' statement below on lines 7 and 8.
I will be posting the full code for the trigger as soon as I have it completely cleaned up for the benefit of other developers, hopefully, by the end of the day.
Sorry about the typo as I was writing off the cuff and sometimes I get "shift" happy with my typing. Glad to see you got it working though.
Believe me, it happens to the best of us. =)
As I believe it is only fair to provide my full solution now that I have benefitted from input, here is the full code for the trigger I just completed. Also, even though this post will be marked as the solution post, I will make sure that mcrosby gets the Kudos. Thanks again!