function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Karunakar BuruguKarunakar Burugu 

unknown property error in salesforce

I am trying to learn Wrapper class in Salesforce development but I am getting the below error, could some one please help me te resolve this error.
Below are the error details, Apex code and VFPage code

Error:
Unknown property 'Wrapperclass.lstWrapperDisplay'

VFPage code:
<apex:page controller = "Wrapperclass">
    <apex:form>
        <apex:pageBlock >
            <apex:pageBlockSection>
                <apex:pageBlockTable value="{!lstWrapperDisplay}" var="w">
                    
                    <apex:column headerValue="Action">
                        <apex:inputCheckbox/>
                    </apex:column>
                    
                    <apex:column headerValue="Account Name">
                        {!w.accname}
                    </apex:column>
                    
                    <apex:column headervalue="Account Number">
                        {!w.accnum}
                    </apex:column>
                    
                    <apex:column headervalue="Industry">
                        {!w.accind}
                    </apex:column>
                    
                    <apex:column headervalue="Opportunity Name">
                        {!w.oppname}
                    </apex:column>
                    
                    <apex:column headervalue="Opportunity Amount">
                        {!w.oppamt}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex class
public class Wrapperclass {
   public Wrapperclass(){
    lstAcc = new List<Account>();   
    lstOpp  = new List<Opportunity>();
   }

  //Name, accountnumber,Industry from Account
  //Name, ammount from opportunity
  //Instanitate List for Account, Opportunity
 
 public List<Account> lstAcc {get;set;}
 public List<Opportunity> lstOpp {get;set;}
    
  //List for Wrapper Class
 
 public List<Wrapper> lstw;
    
    public void lstWrapperDisplay(){
        lstAcc = [select name,accountnumber,Industry from Account];
        lstOpp = [select name,amount from Opportunity];
        
        for(integer i=0;i<lstOpp.size();i++){
            lstw.add(new Wrapper(lstAcc[i].name,lstAcc[i].accountnumber, lstAcc[i].Industry,lstOpp[i].name,lstOpp[i].amount));
                }
            //return lstw;
    }
 
    public class Wrapper{
        public String accname {set;get;}
        public String accnum {set;get;}
        public String accind {set;get;}
        public String oppname {set;get;}
        public Decimal oppamt {set;get;}

        
        public Wrapper(String accname, String accnum, String accind, String oppname, Decimal oppamt){
            this.accname = accname;
            this.accnum = accnum;
            this.accind = accind;
            this.oppname = oppname;
            this.oppamt = oppamt;
        }   
    }
}
Best Answer chosen by Karunakar Burugu
Vikash GoyalVikash Goyal
Hi Karunakar,

You have an iteration variable 'lstWrapperDisplay' on your vf page, so to populate this you have 2 options :

1. Define a property with getter and setter in your controller e.g. public list<Wrapper> lstWrapperDisplay{get;set;} :
public class Wrapperclass {
   public List<Account> lstAcc {get;set;}
 public List<Opportunity> lstOpp {get;set;}
    
   public Wrapperclass(){
    lstAcc = new List<Account>();   
    lstOpp  = new List<Opportunity>();
    populateLstWrapperDisplay();
   }

  //Name, accountnumber,Industry from Account
  //Name, ammount from opportunity
  //Instanitate List for Account, Opportunity
    
  //List for Wrapper Class
 
    public List<Wrapper> lstWrapperDisplay{get;set;}
    
    public void populateLstWrapperDisplay(){
        lstAcc = [select name,accountnumber,Industry from Account];
        lstOpp = [select name,amount from Opportunity];
        lstWrapperDisplay = new List<Wrapper>();
        for(integer i=0;i<lstOpp.size();i++){
            lstWrapperDisplay.add(new Wrapper(lstAcc[i].name,lstAcc[i].accountnumber, lstAcc[i].Industry,lstOpp[i].name,lstOpp[i].amount));
        }
    }
 
    public class Wrapper{
        public String accname {set;get;}
        public String accnum {set;get;}
        public String accind {set;get;}
        public String oppname {set;get;}
        public Decimal oppamt {set;get;}

        
        public Wrapper(String accname, String accnum, String accind, String oppname, Decimal oppamt){
            this.accname = accname;
            this.accnum = accnum;
            this.accind = accind;
            this.oppname = oppname;
            this.oppamt = oppamt;
        }   
    }
}


2. Define a getter method in you controller using syntax : 'get' + [YOURVFPAGEVARIABLENAME], So in your case method name will be 'getlstWrapperDisplay' with return type as 'List<Wrapper>'
public class Wrapperclass {
   public Wrapperclass(){
    lstAcc = new List<Account>();   
    lstOpp  = new List<Opportunity>();
   }

  //Name, accountnumber,Industry from Account
  //Name, ammount from opportunity
  //Instanitate List for Account, Opportunity
 
 public List<Account> lstAcc {get;set;}
 public List<Opportunity> lstOpp {get;set;}
    
  //List for Wrapper Class
 
    public List<Wrapper> lstw{get;set;}
    
    public List<Wrapper> getlstWrapperDisplay(){
        lstAcc = [select name,accountnumber,Industry from Account];
        lstOpp = [select name,amount from Opportunity];
        lstw = new List<Wrapper>();
        for(integer i=0;i<lstOpp.size();i++){
            lstw.add(new Wrapper(lstAcc[i].name,lstAcc[i].accountnumber, lstAcc[i].Industry,lstOpp[i].name,lstOpp[i].amount));
                }
        return lstw;
    }
 
    public class Wrapper{
        public String accname {set;get;}
        public String accnum {set;get;}
        public String accind {set;get;}
        public String oppname {set;get;}
        public Decimal oppamt {set;get;}

        
        public Wrapper(String accname, String accnum, String accind, String oppname, Decimal oppamt){
            this.accname = accname;
            this.accnum = accnum;
            this.accind = accind;
            this.oppname = oppname;
            this.oppamt = oppamt;
        }   
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks

All Answers

Vikash GoyalVikash Goyal
Hi Karunakar,

You have an iteration variable 'lstWrapperDisplay' on your vf page, so to populate this you have 2 options :

1. Define a property with getter and setter in your controller e.g. public list<Wrapper> lstWrapperDisplay{get;set;} :
public class Wrapperclass {
   public List<Account> lstAcc {get;set;}
 public List<Opportunity> lstOpp {get;set;}
    
   public Wrapperclass(){
    lstAcc = new List<Account>();   
    lstOpp  = new List<Opportunity>();
    populateLstWrapperDisplay();
   }

  //Name, accountnumber,Industry from Account
  //Name, ammount from opportunity
  //Instanitate List for Account, Opportunity
    
  //List for Wrapper Class
 
    public List<Wrapper> lstWrapperDisplay{get;set;}
    
    public void populateLstWrapperDisplay(){
        lstAcc = [select name,accountnumber,Industry from Account];
        lstOpp = [select name,amount from Opportunity];
        lstWrapperDisplay = new List<Wrapper>();
        for(integer i=0;i<lstOpp.size();i++){
            lstWrapperDisplay.add(new Wrapper(lstAcc[i].name,lstAcc[i].accountnumber, lstAcc[i].Industry,lstOpp[i].name,lstOpp[i].amount));
        }
    }
 
    public class Wrapper{
        public String accname {set;get;}
        public String accnum {set;get;}
        public String accind {set;get;}
        public String oppname {set;get;}
        public Decimal oppamt {set;get;}

        
        public Wrapper(String accname, String accnum, String accind, String oppname, Decimal oppamt){
            this.accname = accname;
            this.accnum = accnum;
            this.accind = accind;
            this.oppname = oppname;
            this.oppamt = oppamt;
        }   
    }
}


2. Define a getter method in you controller using syntax : 'get' + [YOURVFPAGEVARIABLENAME], So in your case method name will be 'getlstWrapperDisplay' with return type as 'List<Wrapper>'
public class Wrapperclass {
   public Wrapperclass(){
    lstAcc = new List<Account>();   
    lstOpp  = new List<Opportunity>();
   }

  //Name, accountnumber,Industry from Account
  //Name, ammount from opportunity
  //Instanitate List for Account, Opportunity
 
 public List<Account> lstAcc {get;set;}
 public List<Opportunity> lstOpp {get;set;}
    
  //List for Wrapper Class
 
    public List<Wrapper> lstw{get;set;}
    
    public List<Wrapper> getlstWrapperDisplay(){
        lstAcc = [select name,accountnumber,Industry from Account];
        lstOpp = [select name,amount from Opportunity];
        lstw = new List<Wrapper>();
        for(integer i=0;i<lstOpp.size();i++){
            lstw.add(new Wrapper(lstAcc[i].name,lstAcc[i].accountnumber, lstAcc[i].Industry,lstOpp[i].name,lstOpp[i].amount));
                }
        return lstw;
    }
 
    public class Wrapper{
        public String accname {set;get;}
        public String accnum {set;get;}
        public String accind {set;get;}
        public String oppname {set;get;}
        public Decimal oppamt {set;get;}

        
        public Wrapper(String accname, String accnum, String accind, String oppname, Decimal oppamt){
            this.accname = accname;
            this.accnum = accnum;
            this.accind = accind;
            this.oppname = oppname;
            this.oppamt = oppamt;
        }   
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Karunakar,

Update your code from below code. It will help you because it works for me:


//Vf page
<apex:page controller= "AccountAndOpportunityWrapperClass">
    
      <apex:form>
        <apex:pageBlock >
            <apex:pageBlockSection title="Accounts">
                <apex:pageBlockTable value="{!wrapperObj.wrapperAccountList}" var="accObj">
                    
                    <apex:column headerValue="Action">
                        <apex:inputCheckbox/>
                    </apex:column>
                    
                    <apex:column headerValue="Account Name">
                        {!accObj.Name}
                    </apex:column>
                    
                    <apex:column headervalue="Account Number">
                        {!accObj.AccountNumber}
                    </apex:column>
                    
                    <apex:column headervalue="Industry">
                        {!accObj.Industry}
                    </apex:column>
                    
                </apex:pageBlockTable>
            </apex:pageBlockSection>
          </apex:pageBlock>
          <apex:pageBlock>
            <apex:pageBlockSection title="Oppotunities">
                <apex:pageBlockTable value="{!wrapperObj.wrapperOpportunitylist}" var="oppObj">
                    
                    <apex:column headerValue="Action">
                        <apex:inputCheckbox/>
                    </apex:column>
                    
                    <apex:column headerValue="Opportunity Name">
                        {!oppObj.Name}
                    </apex:column>
                    
                    <apex:column headervalue="Amount">
                        {!oppObj.Amount}
                    </apex:column>
                    
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


//Apex controller class code
 
public class AccountAndOpportunityWrapperClass {
    Public Wrapper wrapperObj{get;set;}
    public AccountAndOpportunityWrapperClass(){
        List<Account> accountList = new List<Account>();
        List<Opportunity> opportunityList = new List<Opportunity>();
        accountList = [SELECT Name,AccountNumber,Industry FROM Account LIMIT 10000];
        opportunityList = [SELECT Name,Amount FROM Opportunity LIMIT 10000];
        wrapperObj = new Wrapper();
        wrapperObj.wrapperAccountList = accountList;
        wrapperObj.wrapperOpportunitylist = opportunityList;
    }
    public class Wrapper{
        public List<Account> wrapperAccountList{get;set;}
        Public List<Opportunity> wrapperOpportunitylist{get;set;}
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Karunakar BuruguKarunakar Burugu
Thanks Ajay and Vikash, both the solutions worked for me.