0

I have created one VF page which is on Account object to show the Account Hierarchy at the parent level. Basically this VF page will show Contracts of all child Accounts on parent account.

I have added this page as a related list on Account layout.

Also I have added two buttons New Contract & Renew Contract which are part of Salesforce CPQ managed package.

I am trying to create wrapper class so that we can select Multiple Contract record to Renew Contracts from Parent Account.

I have created the hierarchy which shows the contract of child accounts .

enter image description here

But i am unable to create wrapper class for the same. Can someone help me out.

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="{!wrapaccountList}" 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="{!renewContract}" target="_parent"  styleClass="btn" style="text-decoration:none;padding:4px;" />

                <apex:pageBlockTable value="{! wrapaccountList }" var="con"  title="Contract">
                 <apex:column > 
                  <apex:facet name="header">
                        <apex:inputCheckbox id="chkbox" onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>   
                  <apex:inputCheckbox value="{!con.selected}" 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:pageBlockTable>
                <apex:outputLabel value="No records to display" rendered="{!(ISNULL(a.Contracts))}" styleClass="noRowsHeader"></apex:outputLabel>
            </apex:pageBlockSection>
        </apex:repeat>
    </apex:pageBlock>
</apex:outputPanel>
</apex:form>       

Controller

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

    Account baseAccount = [SELECT Id, 
                           ParentId, 
                           Name,
                           Parent.Name,
                           (SELECT ContractNumber,SBQQ__ActiveContract__c,StartDate,EndDate,Contract_renewed__c FROM Contracts),
                           (SELECT Id, Name FROM ChildAccounts)
                           FROM Account 
                           WHERE Id = :acc.id];
    wrapaccountList.add(new wrapAccount(baseAccount));
    //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,EndDate,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.
            wrapaccountList.add(a);

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

//This method is to create a new contract while clicking on the Add contract button 

 public pageReference newContract(){  

    pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/800/e?&retURL=' + acc.id);  

    return pageRef; 

} 

//This method is to renew contract while clicking on the Renew contract button 
public void renewContract() {
selectedAccounts = new List<Account>();

    for(wrapAccount wrapAccountObj : wrapaccountList) {
        if(wrapAccountObj.selected == true) {
            selectedAccounts.add(wrapAccountObj.acc);
        }
    }
}

// public pageReference renewContract(){  

   // pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/800/e?&retURL=' + acc.id);  
//  pageReference p = new pageReference('/apex/SBQQ__RenewContracts');
//    p.setRedirect(true);
//    return p; 

// } 
 //This method is to edit the existing contact record while clicking the Edit link 


public pageReference editContract(){  

    String contractId = Apexpages.currentpage().getParameters().get('contractId');  

    pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/' + contractId + '/e?retURL=' + acc.id);  

    return pageRef;  

}    
// This is our wrapper/container class. A container class is a class, a data structure, or an 
 abstract 
 data type whose instances are collections of other objects. In this example a wrapper class contains 
 both the standard salesforce object Account and a Boolean value
public class wrapaccountList {
    public Account acc {get; set;}
    public Boolean selected {get; set;}

    //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
    public wrapaccountList(Account a) {
        acc = a;
        selected = false;
    }
}    
}

Error:

Need some expert advice on this.

When I am trying to save it is throwing error:

enter image description here

0

I do not see any class named wrapAccount in your code, I do see a class named wrapAccountList, so you line number 4 in your controller should be

public List<AccountHierarchyController.wrapAccountList> wrapaccountList { get; set; }

Instead of

public List<wrapAccount> wrapaccountList { get; set; }


Update:

I see that there are multiple issues with your code and I would really want you to go through the Salesforce Visualforce and Apex developer guide or trailheads.

The below code should compile fine.

The reason for Version 0.0 error is usually you have changed the version of the class to 0. If that is not the case, please delete the existing class and VF page an create a new ones.

Recommendations:

  • Give meaningful names to your classes. => Example accWrapperList as a class name is ambiguous unless your class actually denotes a collection.
  • Read about list methods > You cannot add an Account object type to a List of accWrapperList.
  • I have not understood the approach you are taking to show the details. Lightning provides a tree grid that can help you out but only venture out if you have some experience with LWC or Aura.
  • Read about apex best practices.

    public with sharing class AccountHierarchyController {
    public Account acc { get; private set; }
    public List<AccountWrapper> wrapaccountList { get; set; }
    public List<Account> selectedAccounts { get; set; }
    public ApexPages.StandardController controller { get; set; }
    
    public AccountHierarchyController(ApexPages.StandardController controller) {
        this.acc = (Account) controller.getRecord();
        this.wrapaccountList = new List<AccountWrapper>();
    
        Account baseAccount = [
            SELECT Id, ParentId, 
            Name,Parent.Name, (SELECT ContractNumber,SBQQ__ActiveContract__c,StartDate,EndDate,Contract_renewed__c FROM Contracts), (SELECT Id, Name FROM ChildAccounts)
            FROM Account
            WHERE Id = :acc.id Limit 1
        ];
        wrapaccountList.add(new AccountWrapper(baseAccount));
        //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,EndDate,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.
                wrapaccountList.add(new AccountWrapper(baseAccount));
    
                // Add this Account's children to the query for the next level.
                for (Account child : a.ChildAccounts) {
                    accountsToQuery.add(child.Id);
                }
            }
        }
    }
    
    //This method is to create a new contract while clicking on the Add contract button
    
    public pageReference newContract() {
    
        pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/800/e?&retURL=' + acc.id);
    
        return pageRef;
    
    }
    
    //This method is to renew contract while clicking on the Renew contract button
    public void renewContract() {
        selectedAccounts = new List<Account>();
    
        for (AccountWrapper wrapAccountObj : wrapaccountList) {
            if (wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
    }
    
    public pageReference editContract() {
    
        String contractId = Apexpages.currentpage().getParameters().get('contractId');
    
        pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/' + contractId + '/e?retURL=' + acc.id);
    
        return pageRef;
    
    }
    
    public class AccountWrapper {
        public Account acc { get; set; }
        public Boolean selected { get; set; }
    
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public AccountWrapper(Account a) {
            acc = a;
            selected = false;
        }
    }
    

    }

  • Corrected that Manjit.. then also I am getting error: 1.Invalid api version:0.0 at line 0 2.Method does not exist or incorrect signature: void add(Account) from the type List<AccountHierarchyController.wrapaccountList> at line 34 3.Invalid type: wrapAccount at line 58 4.Variable does not exist: wrapAccountObj at line 59 & 60 – vd123 Mar 16 '20 at 9:32
  • @vd123 I have made some recommendations – manjit5190 Mar 17 '20 at 7:53
  • i tried the above code.. it is showing me Variable does not exist: wrapaccountList & selectedAccounts – vd123 Mar 17 '20 at 8:09
  • Are you sure you have copied the complete code. – manjit5190 Mar 17 '20 at 8:13
  • Controller got saved .. it is throwing error on VF page " Unknown property 'AccountStandardController.wrapaccountList' " – vd123 Mar 17 '20 at 8:50

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.