0

All check boxes shown as false in the 'Validate' method even though I have selected few. Can't see the reason why. Created a wrapper class on picklist values of a object field and not any object. Is that the issue ?

VF Page

<apex:page standardcontroller="Opportunity" title="Integrate Portfolio" extensions="TestClass">
  <apex:pageblockbuttons >
    <apex:commandbutton action="{!validate}" title="Validate" />
  </apex:pageblockbuttons>
  <apex:pageblocksection title="Group Practice List" columns="1">
    <apex:pageblocktable style="width:100%" value="{!AllGroupPractices}" var="a">
      <apex:column style="width: 5%; text-align: center;">
        <apex:inputcheckbox value="{!a.isSelected}"/>
      </apex:column>
      <apex:column style="width:95%"  value="{!a.groupPractice}"/>
    </apex:pageblocktable>
  </apex:pageblocksection>
</apex:page>

Apex Controller

public with sharing class TestClass{

  // Wrapper class 
  public class groupPracticeWrapper{
    public Boolean isSelected {get; set;}
    public String groupPractice {get; set;}

    public groupPracticeWrapper (String groupPractice){
       this.groupPractice = groupPractice;
       isSelected = false;
    }
  } 


   // Get all Values and set in VF page
   public List<groupPracticeWrapper> getAllGroupPractices(){
      List<groupPracticeWrapper> gpValues = new List<groupPracticeWrapper>();

    //Getting picklist values from an object field
      List<Schema.PicklistEntry> values = fieldMap.get(fld).getDescribe().getPickListValues();
      for (Schema.PicklistEntry a : values){
         gpValues.add(new groupPracticeWrapper(a.getValue()));
      }
     return gpValues;
   }

  // Get all the values that has been ticked
   public PageReference validate(){
     for (groupPracticeWrapper gp : getAllGroupPractices()) {
        System.debug('Grouppactice Selected>>>'+gp.isSelected);
     }
     return null;
   }
}

2 Answers 2

2

Just consolidated the code with changes that @Keith C has suggested. Now you can use this working sample.

Apex Page:

<apex:page standardController="Opportunity" title="Integrate Portfolio" extensions="exampleCon">
    <apex:form>
        <apex:pageBlock>
            <apex:pageblockbuttons >
                <apex:commandbutton action="{!validate}" title="Validate" value="Validate" />
            </apex:pageblockbuttons>
            <apex:pageblocksection title="Group Practice List" columns="1">
                <apex:pageblocktable style="width:100%" value="{!AllGroupPractices}" var="a">
                    <apex:column style="width: 5%; text-align: center;">
                        <apex:inputcheckbox value="{!a.isSelected}"/>
                    </apex:column>
                    <apex:column style="width:95%"  value="{!a.groupPractice}"/>
                </apex:pageblocktable>  
            </apex:pageblocksection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class exampleCon {

    public exampleCon(ApexPages.StandardController stdController){}

    // Wrapper class 
    public class groupPracticeWrapper{
        public Boolean isSelected {get; set;}
        public String groupPractice {get; set;}

        public groupPracticeWrapper (String groupPractice){
           this.groupPractice = groupPractice;
           isSelected = false;
        }
    } 

    public List<groupPracticeWrapper> allGroupPractices {
      get {
          // Create list once only
          if (allGroupPractices == null) {
              System.debug('--This list will be initialized only once--');
              allGroupPractices = new List<groupPracticeWrapper>();
              Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Opportunity.fields.getMap();
              List<Schema.PicklistEntry> values = fieldMap.get('StageName').getDescribe().getPickListValues();

              for (Schema.PicklistEntry a : values){
                  allGroupPractices.add(new groupPracticeWrapper(a.getValue()));
              }
          }
          return allGroupPractices;
      }
      set;
    }

    // Get all the values that has been ticked
    public PageReference validate(){     
      for (groupPracticeWrapper gp : allGroupPractices) {
        System.debug('Grouppactice Selected>>>'+gp.isSelected);
      }

     return null;
   }
}
0
2

Each time you call getAllGroupPractices() a new list of non-selected wrappers is created.

Instead use a property where the list is created once only:

public List<groupPracticeWrapper> allGroupPractices {
    get {
        // Create list once only
        if (allGroupPractices == null) {
            allGroupPractices = new List<groupPracticeWrapper>();
            List<Schema.PicklistEntry> values = fieldMap.get(fld).getDescribe().getPickListValues();
            for (Schema.PicklistEntry a : values){
                allGroupPractices.add(new groupPracticeWrapper(a.getValue()));
            }
        }
        return allGroupPractices;
    }
    set;
}

so that the values are set in the same list that you later iterate over in the validate method:

public PageReference validate(){
    for (groupPracticeWrapper gp : allGroupPractices) {
        System.debug('Grouppactice Selected>>>'+gp.isSelected);
    }
    return null;
}
2
  • Tried.. But still getting the same..
    – Brav
    Jun 7, 2018 at 22:35
  • @brav Suggest you append to your question this v2 of the code (keep what is there at the moment too).
    – Keith C
    Jun 7, 2018 at 23:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .