1

I'm trying to create a VF page to display Account hierarchy and the relative opportunities.

Controller

public class AccountHierarchyCtrl{

    public MyAccount parentAcc {get;set;}
    public Map<Id, List<Account>> childrenPerParent {get;set;}
    public Account accnt {get;set;}

    public class MyAccount {


      public Account acc {get;set;}
      public List<Opportunity> optyList {get;set;}
      public List<MyAccount> children {get;set;}

    public MyAccount(Account acc) {
        this.acc = acc;
        optyList = new List<Opportunity>();
        children = new List<MyAccount>();
    }

    public Boolean addChild(Id parentId, Account child) {
        if(acc.Id == parentId) {
            children.add(new MyAccount(child));
            return true;
        } else {
            Boolean result = false;
            Integer i = 0;
            do {
                MyAccount myacc = children[i];
                result = myacc.addChild(parentId, child);
                i++;
            } while(!result && i < children.size());
        }
        return false;
    }

    public Set<Id> getChildrenId() {
        Set<Id> accIds = new Set<Id>();
        for(MyAccount myacc : children) {
            accIds.add(myacc.acc.Id);
            accIds.addAll(myacc.getChildrenId());
        }
        return accIds;
    }

    public List<MyAccount> getChildrenList() {
        List<MyAccount> childrenList = new List<MyAccount>();
        childrenList.addAll(children);
        for(MyAccount myacc : children) {
            childrenList.addAll(myacc.getChildrenList());
        }
        return childrenList;
    }

    public Boolean addOpty(Id accId, Opportunity opty) {
        if(acc.Id == accId) {
            optyList.add(opty);
            return true;
        } else {
            Boolean result = false;
            Integer i = 0;
            do {
                MyAccount myacc = children[i];
                result = myacc.addOpty(accId, opty);
                i++;
            } while(!result && i < children.size());
        }
        return false;
    }
}

public AccountHierarchyCtrl(ApexPages.StandardController Ctrl){
    this.accnt = (Account)Ctrl.getRecord();

    for(Account acc : [SELECT ParentId, recordtype.name, BillingCity, BillingCountry FROM Account WHERE ParentId != null]) {
    if(childrenPerParent.get(acc.ParentId) == null)
        childrenPerParent.put(acc.ParentId, new List<Account>());
    childrenPerParent.get(acc.ParentId).add(acc);
    }

    parentAcc =new MyAccount(accnt);
    getChildren(accnt.Id);

    Set<Id> accIds = parentAcc.getChildrenId();
    accIds.add(accnt.Id);

    for(Opportunity opty : [SELECT AccountId, Name, Opportunity_Code__c, Amount,   StageName FROM Opportunity 
    WHERE AccountId IN :accIds 
    AND (Recordtype.developername='Child_Opportunity' OR    Recordtype.developername='Child_Opportunity') 
    AND (StageName='Draft - Low' OR StageName='Bid Project - Medium'OR StageName='Approved Project - High' OR StageName='Awarded awaiting implement. - Forecast') ]) {
    parentAcc.addOpty(opty.AccountId, opty);
    }

    List<MyAccount> myAccList = new List<MyAccount>();
    myAccList.add(parentAcc);
    myAccList.addAll(parentAcc.getChildrenList());

    } 



    public void getChildren(Id accId) {
    if(childrenPerParent.get(accId) != null) {
        for(Account a : childrenPerParent.get(accId)) {
            parentAcc.addChild(accId, a);
            getChildren(a.Id);
        }
    }




     } 
}

Now I'm trying to create my visualfroce page, but I can't access to my controller Lists. I get this error:

Error: Unknown property 'AccountStandardController.children'

Page

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

     <apex:pageBlock title="{!Account.Name}" >
     <apex:pageBlockTable value="{!children}"  var="lines" id="theTable" >

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

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

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

   <apex:column headerValue="City" style="align: center;">
                 <apex:outputField value="{!lines.Account.BillingCity}" style="width: 150px;"/>
            </apex:column>                     
    <apex:column headerValue="Opportunity" style="align: center;">
    <apex:pageBlockTable value="{!optyList}" var="l">
    <apex:column headerValue="City" style="align: center;">
                 <apex:outputField value="{!l.acc.Name}" style="width: 150px;"/>
            </apex:column>  
    </apex:pageBlockTable>
    </apex:column>
</apex:pageblockTable>

</apex:pageBlock>

</apex:page>
2

The variable children is an attribute of the MyAccount class. You're referencing it as if it's an attribute of the controller itself. In your PageBlockTable your value should be {!parentAcc.children}.

| improve this answer | |

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.