0

Account is one object and Contract is another object.

I want to show Contracts of all Child Accounts on Ultimate Parent Account.

Below is the code for same.

Controller

public with sharing class AccountHierarchyController {
public Account acc { get; private set; }
public List<Account> accountList { get; private set; }
public ApexPages.StandardController controller { get; private set; }
  public AccountHierarchyController(ApexPages.StandardController controller) {
    this.acc = (Account)controller.getRecord();
    this.accountList = new List<Account>();

    Account baseAccount = [SELECT Id, 
                           ParentId, 
                           Name,
                           Parent.Name,
                           (SELECT ContractNumber,SBQQ__ActiveContract__c,StartDate,Contract_renewed__c FROM Contracts),
                           (SELECT Id, Name FROM ChildAccounts)
                           FROM Account 
                           WHERE Id = :acc.id];
    accountList.add(baseAccount);

    // Traverse the hierarchy downwards
    Set<Id> accountsToQuery = new Map<Id, Account>(baseAccount.ChildAccounts).keySet();
    while (accountsToQuery.size() > 0) {
        List<Account> thisLevelAccounts = [SELECT Id, Name, Parent.Name,
                                           (SELECT ContractNumber,SBQQ__ActiveContract__c,StartDate,Contract_renewed__c FROM Contracts),
                                           (SELECT Id, Name FROM ChildAccounts)
                                           FROM Account
                                           WHERE Id IN :accountsToQuery];
        accountsToQuery = new Set<Id>();

        for (Account a : thisLevelAccounts) {
            // Add this Account (with its Contracts) to the list.
            accountList.add(a);

            // Add this Account's children to the query for the next level.
            for (Account child : a.ChildAccounts) {
                accountsToQuery.add(child.Id);
            }
        }
    }
}
}

Vf Page

<apex:page title="Contract" standardController="Account" extensions="AccountHierarchyController">
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID){
    var inputCheckBox = document.getElementsByTagName("input");
    for(var i=0; i<inputCheckBox.length; i++){
        if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
            inputCheckBox[i].checked = obj.checked;
        }
   }
}
</script>    
<apex:form >
<apex:outputPanel id="cont">
    <apex:pageBlock title="Contracts">


        <apex:repeat value="{! accountList }" var="a">
            <apex:pageBlockSection title="{! a.Name + IF(NOT(ISBLANK(a.ParentId)), ' (child of ' + a.Parent.Name + ')', '') }">

             <apex:commandLink value="New Contract" action="{!newContract}" target="_parent"  styleClass="btn" style="text-decoration:none;padding:4px;" />

             <apex:commandLink value="Renew Contract" action="{!newContract}" target="_parent"  styleClass="btn" style="text-decoration:none;padding:4px;" />

                <apex:pageBlockTable value="{! a.Contracts }" var="con"  title="Contract">
                 <apex:column > 
                  <apex:facet name="header">
                        <apex:inputCheckbox id="chkbox" onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>   
                  <apex:inputCheckbox id="inputId" />    
                 </apex:column>  
                 <apex:column HeaderValue="Action" width="60"> 
                    <apex:commandLink value="Edit" action="{!editContract}" target="_parent"><apex:param value="{!con.id}" name="contractId"/> </apex:commandLink>
                 </apex:column>
                    <apex:column headerValue="Contract Number"><apex:outputLink value="/{!con.id}" target="_blank">{!con.ContractNumber}</apex:outputLink> </apex:column>
                    <apex:column value="{!con.SBQQ__ActiveContract__c}" />
                    <apex:column value="{!con.StartDate}" />
                    <apex:column value="{!con.EndDate}" />
                    <apex:column value="{!con.Contract_renewed__c}" /> 


                <apex:outputLabel value="No records to display" rendered="{!(ISNULL(a.Contracts))}" styleClass="noRowsHeader"></apex:outputLabel>
                </apex:pageBlockTable>

            </apex:pageBlockSection>
        </apex:repeat>
    </apex:pageBlock>
 </apex:outputPanel>
</apex:form>       
</apex:page>

I want to add checkbox in front of each contract so that I can select one record or multiple records at a time.. how to achieve this??

2
  • Can you provide the full code of your Apex class? Feb 12 '20 at 10:34
  • I have modified the code .. and its working fine now.. !!
    – vd123
    Feb 13 '20 at 5:03
1

Your Apex controller appears to be missing a class declaration and all of its member variable declarations. What you have here is only a constructor, which Apex is not expecting to see outside the scope of a class.

You'll need to write the class definition and the declarations of the instance variables acc and accountList.

2
  • It worked..!! Thanks
    – vd123
    Feb 13 '20 at 5:51
  • if I want to add checkbox in front of each contract so that I select the records.. how to achieve this??
    – vd123
    Feb 13 '20 at 5:53

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.