Visualforce checkbox functionality - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Steve PSteve P 

Visualforce checkbox functionality

I have a visualforce page with 2 checkboxes and a View button. The apex controller has 2 functions. When the view button is clicked I want to call the functions based on if the checkboxes are checked or not. How can I achieve that?

Visualforce:
<apex:inputCheckbox label="Box1" title="Box1" selected="true" value="{!Box1Checked} />
<apex:outputText> Box1 </apex:outputText> 

<apex:inputCheckbox label="Box2" title="Box2" selected="true" value="{!Box2Checked}" />
<apex:outputText> Box2 </apex:outputText>

<apex:commandButton action="{!VFRef}" value="View"/>

Apex:
​​​​​​​
public class Main{
    
    public Maps(ApexPages.StandardController controller){
        function1(x, y);
        function2(x, y);
    }
    
    public PageReference VFRef(){
        function1(x, y);
        function2(x, y);
        
    }
    
    function1(x, y){
        //code;
    }
    function2(x, y){
        //code;
        
    }
}

​​​​​​​
Best Answer chosen by Steve P
Ankush AgarwalAnkush Agarwal
Updated one:
 
public class Main{
    
	Boolean Box1Checked {
    get{ 
       if ( Box1Checked == null ){
          Box1Checked = false;
       }
       return Box1Checked;
     } set; }

	Boolean Box2Checked {
    get{
       if ( Box2Checked == null ){
          Box2Checked = false;
       }
       return Box2Checked;
    } set; } 
	
    public Maps(ApexPages.StandardController controller){
        if(Box1Checked) {
			function1(x, y);
		}
		if(Box2Checked) {
			function2(x, y);
		}
    }
    
    public PageReference VFRef(){
        function1(x, y);
        function2(x, y);
        
    }
    
    function1(x, y){
        //code;
    }
    function2(x, y){
        //code;
        
    }
}

 

All Answers

Ankush AgarwalAnkush Agarwal
Hi Steve,

You may try with below updated apex class:
 
public class Main{
    
	Boolean Box1Checked {get;set;}
	Boolean Box2Checked {get;set;}
	
    public Maps(ApexPages.StandardController controller){
        if(Box1Checked) {
			function1(x, y);
		}
		if(Box2Checked) {
			function2(x, y);
		}
    }
    
    public PageReference VFRef(){
        function1(x, y);
        function2(x, y);
        
    }
    
    function1(x, y){
        //code;
    }
    function2(x, y){
        //code;
        
    }
}

 
Steve PSteve P
Hi Ankush,
Thanks for your help.

I am getting a Attempt to de-reference a null object error. I think I am getting this error because the Box1Checked and Box2Checked values are null as they have not been initialized. I need to capture those values from the Visualforce page and I am not sure how.
Ankush AgarwalAnkush Agarwal
Hi Steve,

You may try below:

I have initialized both the properties.
public class Main{
    
	Boolean Box1Checked {
    get{ 
       if ( Box1Checked == null ){
          Box1Checked = false;
       }
     } set; }

	Boolean Box2Checked {
    get{
       if ( Box2Checked == null ){
          Box2Checked = false;
       }
​​​​​​​    } set; } 
	
    public Maps(ApexPages.StandardController controller){
        if(Box1Checked) {
			function1(x, y);
		}
		if(Box2Checked) {
			function2(x, y);
		}
    }
    
    public PageReference VFRef(){
        function1(x, y);
        function2(x, y);
        
    }
    
    function1(x, y){
        //code;
    }
    function2(x, y){
        //code;
        
    }
}

 
Ankush AgarwalAnkush Agarwal
Updated one:
 
public class Main{
    
	Boolean Box1Checked {
    get{ 
       if ( Box1Checked == null ){
          Box1Checked = false;
       }
       return Box1Checked;
     } set; }

	Boolean Box2Checked {
    get{
       if ( Box2Checked == null ){
          Box2Checked = false;
       }
       return Box2Checked;
    } set; } 
	
    public Maps(ApexPages.StandardController controller){
        if(Box1Checked) {
			function1(x, y);
		}
		if(Box2Checked) {
			function2(x, y);
		}
    }
    
    public PageReference VFRef(){
        function1(x, y);
        function2(x, y);
        
    }
    
    function1(x, y){
        //code;
    }
    function2(x, y){
        //code;
        
    }
}

 
This was selected as the best answer