+ Start a Discussion
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11 

How to Pass input Checkbox values from visualforce page to controller

Initially I have set all the InputCheckbox values to false. And shown the values on PageblockTable in VFP. When user changes the checkbox values and click submit, I wanted to get all the new Checkbox values. How can i do that.
 
//VFP:

<apex:page controller="Testctrl">
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!attdmap}" var="attdkey">
        
            <apex:column value="{!attdkey.Name}"/>
            <apex:column>
                <apex:facet name="header">Absent</apex:facet>
                <apex:inputCheckbox label="Absent" value="{!attdmap[attdkey]}" />
            </apex:column>

        </apex:pageBlockTable>
    </apex:pageBlock>
    
    <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>



//CTRL:

public class Testctrl {
    public list<Student__c> acctlist {get;set;}
    public Map<Student__c, Boolean> attdmap{get;set;}
     
    public Testctrl(){
        attdmap = new Map<Student__c, Boolean>();
        acctlist = [SELECT Id, Name FROM Student__c];

        for(Student__c atu23 : acctlist){
        attdmap.put(atu23,False);
        }
       
    }
    public void submit(){
    }

}

 
Best Answer chosen by Vijay Kumar Rebbala 11
pconpcon
This is pretty out of scope for your original question, but I'm going to look ahead and make some assumptions for what you're trying to do.  I've created the a Student__c object in my sandbox and an Attendance__c object in my sandbox.  The Student__c object has a Name field and the Attendance__c object has a M-D relationship to the Student__c object, a Abesent checkbox and a Date field.

AttendanceController.cls
public class AttendenceController {
    public class StudentWrapper {
        public Student__c student {
            get;
            set;
        }
        public Boolean absent {
            get;
            set;
        }
        
        public StudentWrapper() {
            this.absent = false;
        }
        
        public StudentWrapper(Student__c student) {
            this();
            this.student = student;
        }
    }
    
    public List<StudentWrapper> students {
        get;
        set;
    }
    
    public Date day {
        get;
        set;
    }
    
    public AttendenceController() {
        students = new List<StudentWrapper>();
        
        for (Student__c student : [
            select Name
            from Student__c
        ]) {
            students.add(new StudentWrapper(student));
        }
    }
    
    public PageReference submit() {
        List<Attendance__c> attendance = new List<Attendance__c>();
        
        for (StudentWrapper wrapper : this.students) {
            attendance.add(new Attendance__c(
                Date__c = this.day,
                Student__c = wrapper.student.Id,
                Absent__c = wrapper.absent
            ));
        }
        
        if (!attendance.isEmpty()) {
            insert attendance;
        }
        
        return null;
    }
}

Attendance.vfp
<apex:page docType="html-5.0" controller="AttendenceController">
    <apex:form>
        <apex:input type="date" value="{!day}" />
        <apex:pageBlock>
            <apex:pageBlockTable value="{!students}" var="wrapper">
                <apex:column value="{!wrapper.student.Name}" />
                <apex:column>
                    <apex:facet name="header">Absent</apex:facet>
                    <apex:inputCheckbox value="{!wrapper.absent}" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>

The page needs some stylization work but it's functional.  After you click submit it will not refresh the page or take you anywhere else (you'll need to do that yourself by returning a valid PageResult instance).  After clicking submit the page will insert all of the Attendance__c objects.

This should at least help you to understand how you can do what I think you're trying to do.  If this does not help, let me know.

All Answers

pconpcon
So you shouldn't use a complex type as the key to a map.  You should do this with a simple type.  For example:
 
public class Testctrl {
    public list<Student__c> acctlist {
        get;
        set;
    }
    public Map<Id, Boolean> attdmap{
        get;
        set;
    }
     
    public Testctrl(){
        this.attdmap = new Map<Id, Boolean>();
        this.acctlist = [
            select Name
            from Student__c
        ];  

        for(Student__c atu23 : acctlist) {
            attdmap.put(atu23.Id, false);
        }
    }
    
    public void submit() {}
}
 
<apex:page controller="Testctrl">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!attdmap}" var="attdkey">
                <apex:column value="{!attdkey.Name}"/>
                <apex:column>
                    <apex:facet name="header">Absent</apex:facet>
                    <apex:inputCheckbox label="Absent" value="{!attdmap[attdkey.Id]}" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>

        <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>

 
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11
@pcon. Thanks for the reply

I got the error when i tried your code.
Incorrect parameter type for subscript. Expected java.lang.Class, received Text
Error is in expression '{!attdmap[attdkey.Id]}' in component <apex:inputCheckbox> in page testpage

So, i removed the ID from 8th line of VFP. and executed. I have added debug log in submit button to check the map values but no result.
 
pconpcon
You may need to change line 6 of the controller to be the following instead
 
Map<String, Boolean> attdmap {
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11
<apex:page controller="Testctrl">
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!attdmap}" var="attdkey">
        
            <apex:column value="{!attdkey.Name}"/>
            <apex:column >
                <apex:facet name="header">Absent</apex:facet>
                <apex:inputCheckbox value="{!attdmap[attdkey.Id]}"> 
                </apex:inputCheckbox>
            </apex:column>

        </apex:pageBlockTable>
    </apex:pageBlock>
    
    <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>


public class Testctrl {
    public list<Student__c> acctlist {get;set;}
    public Map<String, Boolean> attdmap{get;set;}
     
    public Testctrl(){
        this.attdmap = new Map<String, Boolean>();
        this.acctlist = [SELECT Id, Name FROM Student__c];
        for(Student__c atu23 : acctlist){
        attdmap.put(atu23.Name,False);
        }          
    }
    public void submit(){
        system.debug('************maps*****************'+attdmap);
    }

}

Still getting the same error: Unknown property 'String.Id'
Error is in expression '{!attdmap[attdkey.Id]}' in component <apex:inputCheckbox> in page testpage.

Don't you thing Onclick event on inputcheckbox can send the values to the controller. Tried in this way but no expected results. I might be missing something
pconpcon
Sorry, this is totally my fault.  I assumed you were iterating over acclist instead.  This is what your page should be
 
<apex:page controller="Testctrl">
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!acctlist}" var="acct">
        
            <apex:column value="{!acct.Name}"/>
            <apex:column >
                <apex:facet name="header">Absent</apex:facet>
                <apex:inputCheckbox value="{!attdmap[acct.Id]}"> 
                </apex:inputCheckbox>
            </apex:column>

        </apex:pageBlockTable>
    </apex:pageBlock>
    
    <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>

 
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11
I made the changes but no result. How in the VFP you are saving the User input checkbox values that has to pass to MAP in controller.
pconpcon
This is pretty out of scope for your original question, but I'm going to look ahead and make some assumptions for what you're trying to do.  I've created the a Student__c object in my sandbox and an Attendance__c object in my sandbox.  The Student__c object has a Name field and the Attendance__c object has a M-D relationship to the Student__c object, a Abesent checkbox and a Date field.

AttendanceController.cls
public class AttendenceController {
    public class StudentWrapper {
        public Student__c student {
            get;
            set;
        }
        public Boolean absent {
            get;
            set;
        }
        
        public StudentWrapper() {
            this.absent = false;
        }
        
        public StudentWrapper(Student__c student) {
            this();
            this.student = student;
        }
    }
    
    public List<StudentWrapper> students {
        get;
        set;
    }
    
    public Date day {
        get;
        set;
    }
    
    public AttendenceController() {
        students = new List<StudentWrapper>();
        
        for (Student__c student : [
            select Name
            from Student__c
        ]) {
            students.add(new StudentWrapper(student));
        }
    }
    
    public PageReference submit() {
        List<Attendance__c> attendance = new List<Attendance__c>();
        
        for (StudentWrapper wrapper : this.students) {
            attendance.add(new Attendance__c(
                Date__c = this.day,
                Student__c = wrapper.student.Id,
                Absent__c = wrapper.absent
            ));
        }
        
        if (!attendance.isEmpty()) {
            insert attendance;
        }
        
        return null;
    }
}

Attendance.vfp
<apex:page docType="html-5.0" controller="AttendenceController">
    <apex:form>
        <apex:input type="date" value="{!day}" />
        <apex:pageBlock>
            <apex:pageBlockTable value="{!students}" var="wrapper">
                <apex:column value="{!wrapper.student.Name}" />
                <apex:column>
                    <apex:facet name="header">Absent</apex:facet>
                    <apex:inputCheckbox value="{!wrapper.absent}" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>

The page needs some stylization work but it's functional.  After you click submit it will not refresh the page or take you anywhere else (you'll need to do that yourself by returning a valid PageResult instance).  After clicking submit the page will insert all of the Attendance__c objects.

This should at least help you to understand how you can do what I think you're trying to do.  If this does not help, let me know.
This was selected as the best answer
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11
Thanks pcon, that worked.
PRANAB KISHORE PANDAPRANAB KISHORE PANDA
Hi pcon, can you please help . How can I pass checkbox value from VF to controller .Please help

<apex:page controller="DisplayOptyStage"> <apex:form > <apex:pageBlock > <apex:inputCheckbox label="AA" value="{!opp.stagename}"/> <apex:commandButton VALUE="CLICK HERE" action="{!DisplayOptyStage1}"/> <apex:pageBlockTable value="{!opty1}" var="v"> <apex:column value="{!v.Name}"/> <apex:column value="{!v.Amount}"/> <apex:column value="{!v.StageName}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>

APEX CLASS
public class DisplayOptyStage{

public Opportunity  opp{get;set;}
public List<Opportunity>  opty1{get;set;}



public DisplayOptyStage() {
opp=new Opportunity();
opty1=new List<Opportunity>();
}



public pagereference DisplayOptyStage1()

   
  {
 
  system.debug('--opp.stagename--'+opp.stagename);
opty1=[select Name,Amount,stagename from opportunity where stagename =:opp.stagename];
return null;
}
PRANAB KISHORE PANDAPRANAB KISHORE PANDA
Hi pcon, i am trying to create 2 checkbox(stage =closed own and stage =qualification  on OPTY VF page 
pconpcon
The problem is that the field you have it bound to is a String type not a Boolean type.  If you are trying to may a list of opportunities based on a series of checkboxes, you can do it like this
 
<apex:page controller="DisplayOptyStage">
    <apex:form >
        <apex:pageBlock >
            <apex:inputCheckbox label="Closed Won" value="{!closedWon}" />
            <apex:inputCheckbox label="Qualification" value="{!qualification}" />

            <apex:commandButton value="List Opportunities" action="{!listOpportunities}" reRender="opportunityTable" />
            <apex:pageBlockTable id="opportunityTable" value="{!oppList}" var="opp">
                <apex:column value="{!opp.Name}"/>
                <apex:column value="{!opp.Amount}"/>
                <apex:column value="{!opp.StageName}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Visualforce page
 
public class DisplayOptyStage {
    /** If we should look for the Closed Won stage */
    public Boolean closedWon {
        get;
        set;
    }

    /** If we should look for the Qualification stage */
    public Boolean qualification {
        get;
        set;
    }

    /** List of opportunities to display */
    public List<Opportunity> oppList {
        get;
        set;
    }

    /**
    * Constructor
    */
    public DisplayOptyStage() {
        this.oppList = new List<Opportunity>();
    }

    /**
    * Gets the stage names to look for
    *
    * @return The stage names
    */
    private List<String> getStageNames() {
        List<String> results = new List<String>();

        if (this.closedWon) {
            results.add('Closed Won');
        }

        if (this.qualification) {
            results.add('Qualification');
        }

        return results;
    }

    /**
    * Updates the list of opportunities based on the selected stages
    *
    * @return null since we do not want to go anywhere
    */
    public PageReference listOpportunities() {
        this.oppList = [
            select Amount,
                Name,
                StageName
            from opportunity
            where Stagename = :this.getStageNames()
        ];

        return null;
    }
}
Controller

You will want to add error handling incase someone unchecks both boxes since that will give you bad results.

NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.