You need to sign in to do that
Don't have an account?
ppiyush
Ultimate parent for Accounts, Contacts, Projects
Hello!
We are in the professional services business, and we often work with organizations with multi-level hierarchies (3 to 4 levels is very average). I was wondering if anyone knows how one can create a script to automatically find out the ultimate parent of any account / contact / project, and store the result in a field on every entity.
this would allow us to create savvy rollups in our reporting and dashboarding. any help will be much appreciated.
thanks,
Pranav
I could see doing this with an Apex Trigger, yes. However, a lookup field on a record cannot point to that record itself. So a whole hierarchy may have lookups to the one "top parent" Account, but that Account will not be included. In SF Reports, that Account will not be sorted or grouped with all of its children and grandchildren.
However, this may not be a problem for your use. In that case, I would build a Trigger to handle inserts and updates, and use some kind of batch process to catch up the existing Accounts.
Jeremy
All Answers
We have a formula field something like that for our Account hierarchies.
This goes up to two levels up, but you should be able to extend it to 3 or 4. Of course, you can use Name instead of ID if you like. If you use this field to Group By in your reports, it will keep the parent with all of its children together. If you do that, using Name will probably be easier on the eyes.
Jeremy
Hi Jeremy,
that was very helpful. By doing this, I was indeed able to get an ultimate parent field on the account field.
I noticed that a formula field can only be of type number, text etc... Can't I create a LOOKUP field on Accounts? That way I would be able to refer to the ultimate parent in much easier ways - even in reporting - I dont have to group by - instead I can just say all accounts with Parent Account = certain account...
Any way of programming this in apex?
Best,
Pranav
I could see doing this with an Apex Trigger, yes. However, a lookup field on a record cannot point to that record itself. So a whole hierarchy may have lookups to the one "top parent" Account, but that Account will not be included. In SF Reports, that Account will not be sorted or grouped with all of its children and grandchildren.
However, this may not be a problem for your use. In that case, I would build a Trigger to handle inserts and updates, and use some kind of batch process to catch up the existing Accounts.
Jeremy
thanks jeremy - thats actually exactly what i have been working on - and your reply confirms that this is the right way of doing it.
how do you do batch processes in salesforce?
The Apex Developers Guide (included in the Help Contents with Eclipse IDE) is a good place to start. Search for "batch apex" in Eclipse Help.
Jeremy
Jeremy,
I have developed the following code, but for some reason, nothing inside the execute method is getting executed! :) any guidance would be great...
Controller for my visual force page:
visualforce page:
Any clue?
I haven't worked with Batches a whole lot, but I wonder if you actually have any data to run through. If your query results are null, the execute method will not run.
Also, try replacing the variable "query" with the actual text of your query with [] square brackets around it, and see if that makes a difference.
Good luck,
Jeremy
Pranav - you need to perform a DML update in your Execute method - otherwise no changed will be committed to the DB and it'll seem as if the Batch didn't run at all (even though it did).
Change your execute method code as such:
thanks - that worked! :) silly me...
Check out this formula that I found if you want to use clicks instead of code. It accommodates a hierachy 10 levels deep.