Some of my Accounts are known as Ultimate Parents. They are either parent or grandparent to all other Accounts for a particular company. Due to a large data migration, I would like to have the Ultimate Parent name in a field (UPName__c) on every Account. Unfortunately, this was not done when the system was initially put together, so I now must somehow put together a way to scan through all of my Accounts and get that information.

Ultimate Parent accounts have a checkbox checked to indicate they are an Ultiamte Parent (UltimateParent_c = "Yes"). So I'm thinking I would need to query the ParentAccount_r.UltimateParent_c of every Account record and if the UltimateParent_c = "Yes" then I would grab the Name field and put it into my UPName__c field.

Somehow I have to account for grandchildren accounts. Their ParentAccount will not be the Ultimate Parent, so I don't know how to obtain the Ultiamte Parent name. I'm guessing I would need to put together a query and run it a bunch of times, with the following runs checking to see if the Parent Account has UPName_c field populated, and if it does to grab the value and put it in the current Accounts UPName_c field...?

How would I go about doing this ? Will it be code which would be run in the developer console ?

Thank you very much for any help you can give. I greatly appreciate it.


You should be able to do this with a cross-object formula field, I don't think that a code-based solution is needed. The field would look up the chain of the account hierarchy for your field:

IF ( AND(Parent.Parent.Id  != null,Parent.Parent.UltimateParent_c = 'Yes'), Parent.Parent.Name ,
   IF (AND(Parent.Id  != null,Parent.UltimateParent_c = 'Yes'), Parent.Name , null )
  • would I be able to just run that code through Execute Anonymous on Eclipse and have it execute on all the Account records ? – Zoom_v Sep 26 '13 at 13:40
  • 1
    There is no code here, this is a formula field. It will work as soon as you create the field. – greenstork Sep 26 '13 at 16:39
  • Yes, I was able to figure out that I would need to do a batch update in order to accomplish this. However, my batch code isn't updating my records. I have put the code here if you would care to take a look at it. If you want to give some feedback I would appreciate it very much : salesforce.stackexchange.com/questions/17328/… Thank you greenstork – Zoom_v Sep 26 '13 at 18:24

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.