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
sfdcFanBoysfdcFanBoy 

Pageblock Checkbox value not captured in wrapper class

I have a pageblock with 2 columns and a ceckbox column.. when I select the checkbox, the value is not being captured. Yes im using the wrapper class as per the salesforce example. 

In my code, the list "selectedEngs" is empty.  To this list, Im adding the selected rows.  But the size is zero, even after checkbox is ticked.

Did I miss anything, below is my code.  Please let me know.


VF Page: 2 columns. one is checkbox
<apex:repeat value="{!Engagements}" var="item">

<apex:pageBlockSection id="Selected_PBS" columns="3" >                                                                          <apex:outputPanel layout="block">
             <label>Program</label>
             <apex:outputText value="{!item.eng.Program__c}"/>
        </apex:outputPanel>
                                
        <apex:outputPanel layout="block">                              
              <apex:inputCheckbox value="{!item.selected}" id="checkedone">
              <apex:actionSupport event="onclick" action="{!GetSelected}" rendered="false"/>                                          </apex:inputCheckbox>
         </apex:outputPanel>                                                           
 </apex:pageBlockSection>

</apex:repeat>


Apex class
List <Engagement__c> selectedEngs = new list<Engagement__c>();
List<EngagementWrapper> engList = new List<EngagementWrapper>();
     
     public PageReference getSelected()
     {
        selectedEngs.clear();
        for(EngagementWrapper engwrapper : engList)
        {
        if(engwrapper.selected == true)
        selectedEngs.add(engwrapper.eng);
        }
        return null;

    }
    public List<EngagementWrapper> getEngagements()
    {    
        engList.clear();
        for(Engagement__c e : studentList)
        engList.add(new EngagementWrapper(e));

        return engList;

    }
    
      public List<Engagement__c> GetSelectedEngs()
     {
        if(selectedEngs.size()>0)
        return selectedEngs;
        else
        return null;
     }


Cheers!

Chidambar ReddyChidambar Reddy
Hello Manish

This must be due to one of the following reasons
public PageReference getSelected()
{
        //selectedEngs.clear();
        for(EngagementWrapper engwrapper : engList)
        {
        if(engwrapper.selected == true)
        selectedEngs.add(engwrapper.eng);
        }
        return null;

    }
     <apex:outputPanel layout="block">                            
              <apex:inputCheckbox value="{!item.selected}" id="checkedone">
              <apex:actionSupport event="onselect" action="{!GetSelected}" rendered="false"/>                                          </apex:inputCheckbox>
         </apex:outputPanel>

I am not sure why you are  using Action support, you can grab all selected items by iterating wrapper list.

You can remove that action support and you can add that grabbing the selected items code inside the button.


If you want to select some items and then continue to select, you can add one more button where you will get the items and renders the pageblock.



sfdcFanBoysfdcFanBoy
Hey thanks. I did changes as per your suggestion. Still no response. The selected items list size is zero! Its not getting selected.
Chidambar ReddyChidambar Reddy
Have you used List<wrapper> somename {get; set;} property??
sfdcFanBoysfdcFanBoy
Yes, this is the code I used for get set.

public class EngagementWrapper
{
        public Engagement__c eng{get; set;}
        public Boolean selected {get; set;}

        public EngagementWrapper(Engagement__c e)
        {
           eng = e;
           selected = false;
        }
}


Chidambar ReddyChidambar Reddy
This one is fine, but we need to use property for the List<EngagementWrapper> that is being used in Visualdforce page

In the main class :
List<EngagementWrapper> engList {get; set;}


If the above change doesn't work, I suggest you to remove Actionsupport and 

Call the method 'Getselected()' from a command button like 'Add Itmes'



I hope adding the property will solve your issue.
sfdcFanBoysfdcFanBoy
I have defined this before as a list.  I dont think I can make it a property.  

List<EngagementWrapper> engList = new List<EngagementWrapper>();


Chidambar ReddyChidambar Reddy
//Inside the class
public class classname{
List<EngagementWrapper> engList {get; set;}

//in the constructor
Public classname(){
       engList = new List<EngagementWrapper>() ;

}

public void Engagements()
    {   
        engList.clear();
        for(Engagement__c e : studentList)
        engList.add(new EngagementWrapper(e));

 }


}

<apex:page action = "{!Engagements}">

<apex:repeat value={!engList}" var="item">

<apex:pageBlockSection id="Selected_PBS" columns="3" >                                                                          <apex:outputPanel layout="block">
             <label>Program</label>
             <apex:outputText value="{!item.eng.Program__c}"/>
        </apex:outputPanel>
                                
        <apex:outputPanel layout="block">                              
              <apex:inputCheckbox value="{!item.selected}" id="checkedone">
              <!--apex:actionSupport event="onclick" action="{!GetSelected}" rendered="false"/-->                                          </apex:inputCheckbox>
         </apex:outputPanel>                                                           
 </apex:pageBlockSection>

</apex:repeat>
<apex:commandButton value="Add selected" action="{!getselected}"/>
</apex:page>




sfdcFanBoysfdcFanBoy

public PageReference getSelected()
{
        //selectedEngs.clear();
        for(EngagementWrapper engwrapper : engList)
        {
        if(engwrapper.selected == true)
        selectedEngs.add(engwrapper.eng);
        }
        return null;

    }
 I have debugged to the code.  The problem is with the the line  "if(engwrapper.selected == true)".  The value is not being set to TRUE even after selection.

if this is fixed, everything should be working fine.

 I have tried all your suggestions.  I have now removed actionSupport and used that action method code in a command button.  still this is not setting to true.  
Chidambar ReddyChidambar Reddy
<apex:pageBlock id="block" >
        
            <apex:pageBlockButtons >
                
                <apex:commandButton value="Add products" action="{!getSelected}" />
               
            </apex:pageBlockButtons>
            <!-- table -->
            
            <apex:pageBlockTable value="{!engList}" var="e" id="table">
                <apex:column >
                    
                    <apex:inputCheckbox value="{!e.selected}" id="s"/>
                </apex:column>
               
                <apex:column value="{!e.eng.Program__c}" />
                
            </apex:pageBlockTable>
        </apex:pageBlock>
Try changing the page like above

Make sure that you have the property for 'engList'
sfdcFanBoysfdcFanBoy
No change :(
sfdcFanBoysfdcFanBoy
<apex:pageblockTable value="{!Engagements}" var="item" rendered="{!IF(Engagements.Size > 0, true, false)}">
<apex:column>                                  
         <apex:outputPanel layout="block">
                <label>Program Duration</label>
                <apex:outputText value="{!item.eng.Program_Duration__c}"/>
        </apex:outputPanel>
</apex:column>
<apex:column>      
         <apex:outputPanel layout="block" rendered="{!checkHFStatus}">                              
                <apex:inputCheckbox value="{!item.Selected}" id="checkedone">
                <!--<apex:actionSupport event="onchange" action="{!applySelectedFlags}" /> -->                                          </apex:inputCheckbox>
          </apex:outputPanel>  
</apex:column>
</apex:pageblockTable>

<apex:commandLink action="{!saveSelected}" styleClass="navbar-link"  value="Save Selected" reRender="pb,validationMsg" status="waitMsg" shape="">
          <apex:param value="{!sortExpression}" ></apex:param>
</apex:commandLink>


public without sharing class HFController { 

List<Engagement__c> selectedEngs{get;set;}
List<EngagementWrapper> engList {get;set;}

public HFController(){
        engList = new List<EngagementWrapper>();
        selectedEngs  = new List<Engagement__c>();
}

public List<EngagementWrapper> getEngagements()
    {    
        engList.clear();
        for(Engagement__c e : studentList){
            engList.add(new EngagementWrapper(e));
        }
        return engList;

    }

public List<Engagement__c> GetSelectedEngs()
     {
        if(selectedEngs.size()>0)
        return selectedEngs;
        else
        return null;
     } 
     
     public List<Match__c> lstNewSavedMatches = new list<Match__c>();
     
     public PageReference saveSelected()
     {  
            system.debug('******* entered save: ');
                    
        try{            
            System.debug('*****engList size:'+engList.size()); // this size is 1 in debug
            for(EngagementWrapper engwrapper : engList)
            {
                if(engwrapper.selected == true)
                selectedEngs.add(engwrapper.eng);
            }             
            system.debug('******* selectedEngs: '+selectedEngs); //this is NULL as per debug
               
            For(Engagement__c selEng : selectedEngs)
            {  

             *****doing insert ***
             }
            }
       }

}


Chidambar ReddyChidambar Reddy
I did not find any issue here

Try debugging 'engWrapper.selected' in side grabbing the records.
sfdcFanBoysfdcFanBoy
*****engwrapper.selected---after  clicking save:false

captured in save method.
Chidambar ReddyChidambar Reddy
you are getting the 'engList' records by calling a method,

You are not setting the value 'selected' in the list engList.


In the pageblock try this
<apex:pageblockTable value="{!engList}" var="item" rendered="{!IF(engList.Size > 0, true, false)}">


for(Engagement__c e : studentList){
            engList.add(new EngagementWrapper(e));
        }



Do this inside constructor or in a method that is called before or in page action


sfdcFanBoysfdcFanBoy
No chidambar.  that forloop is already in the method.. Anyways I tried adding in action method, no change.


sfdcFanBoysfdcFanBoy
Cannot make engList as the value for pageBlockTable
Harish ChauhanHarish Chauhan
So is There any final solution for this as I am getting the same issue.