Hello I am relatively new to Salesforce and am trying to create a visualforce Account Hierarchy page that displays more than 500 accounts. I'm receiving this error whenever I test out my link on most accounts except for one and I'm hoping someone can point out what it is I am doing wrong

Error Message:

SObject row was retrieved via SOQL without querying the requested field: Account.ParentId An unexpected error has occurred. Your development organization has been notified.


public class LAM_Hierarchy_AP {

    public Account acc { get; private set; }
    public List<Account> accountList { get; private set; }
    public ApexPages.StandardController controller { get; private set; }

    public LAM_Hierarchy_AP(ApexPages.StandardController controller) {
        this.acc = (Account)controller.getRecord();
        this.accountList = new List<Account>();

        Account baseAccount = [SELECT Id, ParentId, Parent.Name, LAM_Shipping_Address__c, Name, Type, OPCO__c, Site_ID__c, Rolling_12_Month_Total_Revenue__c,(SELECT Id, Name FROM ChildAccounts)FROM Account WHERE Id = :acc.id LIMIT 1000];

        // Traverse the hierarchy downwards
        Set<Id> accountsToQuery = new Map<Id, Account>(baseAccount.ChildAccounts).keySet();
        while (accountsToQuery.size() > 0) {
            List<Account> thisLevelAccounts = [SELECT Id, ParentId, Name, Parent.Name, Type, LAM_Shipping_Address__c, OPCO__c, Site_ID__c, Rolling_12_Month_Total_Revenue__c, 
                                              // (Select Id, Name, Phone, Email from Contacts),
                                               (SELECT Id, Name FROM ChildAccounts)
                                               FROM Account
                                               WHERE Id IN :accountsToQuery LIMIT 1000];

            accountsToQuery = new Set<Id>();

            for (Account a : thisLevelAccounts) {
                // Add this Account (with its Contacts) to the list.
                // Add this Account's children to the query for the next level.
                for (Account child : a.ChildAccounts) {
                    if(a.Id == child.ParentId ){




<apex:page standardController="Account" extensions="LAM_Hierarchy_AP">    

        <apex:pageBlock title="{!Account.Name}">

                    <apex:pageBlockTable value="{! accountList }" var="a" id="conlist" title="Contact">

                        <apex:column headerValue="Account Name" style="align: center;">
                              <apex:outputField value="{!a.Name}" style="width: 150px;"/>

                        <apex:column headerValue="Record Type" style="align: center;">
                            <apex:outputField value="{!a.Type}" style="width: 150px;"/>

                        <apex:column headerValue="Rolling 12 Month Total Revenue" style="align: center;">
                            <apex:outputField value="{!a.Rolling_12_Month_Total_Revenue__c}" style="width: 150px;"/>

                        <apex:column headerValue="OPCO" style="align: center;">
                            <apex:outputField value="{!a.OPCO__c}" style="width: 150px;"/>

                        <apex:column headerValue="Site" style="align: center;">
                            <apex:outputField value="{!a.Site_ID__c}" style="width: 150px;"/>

                        <apex:column headerValue="Mailing Address" style="align: center;">
                            <apex:outputField value="{!a.LAM_Shipping_Address__c}" style="width: 150px;"/>



  • Working with hierarchical data in Salesforce can be quite challenging. There's a fundamental conflict between the recursive nature of collecting the related records, and the SOQL governor limit. Good luck! Feb 4, 2019 at 14:03

1 Answer 1


Your problem is here:

(SELECT Id, Name FROM ChildAccounts)

You need to add ParentId to this SELECT clause.

You must log in to answer this question.