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
Roger PavelleRoger Pavelle 

How do I pass the checked/not checked value from a checkbox to a controller function?

My data has 50+ filterable fields that are boolean values.  I am trying to create a function that will filter the data based on whether one of the fields is selected.  My idea is that, in the controller function, I will dynamically build a SOQL WHERE clause based on which fields have been selected.  Basically, when a box is checked, it will rebuild the WHERE clause to include the corresponding field.  If the box is then unchecked, the field will be removed from the WHERE clause.

What I'm not sure about is how to pass the TRUE/FALSE value from the checkbox to the function that will do the building.

In the controller module, I'm planning on having two arrays, one with all the possible where clause strings and another with corresponding boolean values that will determine if the string should be added (the whereArray variable referenced below].  
    public void buildWhere(Integer int, Boolean value)   
    {
           whereArray[int] = value;
    }

So, on the Apex page, how do I go about passing the true/false value to the function?

Thanks.
Best Answer chosen by Roger Pavelle
Roger PavelleRoger Pavelle
I ended up solving this problem using brute force.  I created a list that would hold a boolean value and passed the check box number to that function as a index.  I later build the indicated where clause by looping through the list and adding values when the box value is still true.

Here's the final code:
Apex:
                        <apex:inputCheckbox label="Found in S.W. Pennsylvania" id="regionFoundSWPAChk" >
                            <apex:actionSupport action="{!setChkBoxValue}" event="onchange" rerender="bolete_list">
                                <apex:param name="boxNum" value="49"/>
                            </apex:actionSupport>
                        </apex:inputCheckbox>

VF:
    private List<Boolean> chkBoxValues = new List<Boolean>{false,false,false,false,false,false,false,false,false,false,false,
        false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
        false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
        false,false,false,false,false,false,false,false,false,false,false,false,false,false};

    public void setChkBoxValue()
    {
        integer boxNum = integer.valueOf(system.currentPageReference().getParameters().get('boxNum'));
        boolean before = chkBoxValues[boxNum];
        chkBoxValues[boxNum] = !chkBoxValues[boxNum];
        System.debug('boxNum:' + boxNum + ' before:' + before);
        System.debug(currentChkBoxValues);
    }

    private void buildWhereClause()
    {
        Boolean exists = false;
        whereClause = '';
       
        //The first four values are for edibility, which are not mutually exclusive, so they are built
        // using or instead of and
        for (Integer i = 0; i<4; i++)
        {
            if (chkBoxValues[i])
            {
                if (exists)
                {
                    whereClause = whereClause + ' or (Edibility__c = ' + whereClauseValues[i] + ')';
                }
                else
                {
                    whereClause = 'Where ((Edibility__c = ' + whereClauseValues[i] + ')';
                    exists = true;
                }
            }
        }
        if (exists)
        {
            whereClause = whereClause + ') ';
        }
       
        //All the other values are individual values, so they are built with AND
        for (Integer i = 4; i<whereClauseValues.size(); i++)
        {
            if (chkBoxValues[i])
            {
                if (exists)
                {
                    whereClause = whereClause + 'and ' + whereClauseValues[i] + ' = true ';
                }
                else
                {
                    whereClause = 'Where ' + whereClauseValues[i] + ' = true ';
                    exists = true;
                }
            }           
        }
    }
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Roger Pavelle,

Please check below post. I hope that will help you.
https://developer.salesforce.com/page/Wrapper_Class

Thanks
Amit Chaudhary
KapilCKapilC
Roger,

1 quick question. Do you have this boolean fields within any object?

Thanks,
Kapil
Roger PavelleRoger Pavelle
Picture two sections on a single page.  The top section is all checkboxes and the second section is a table of search results.  Whenever a box in the top section is checked (or subsequently unchecked), the search results in the bottom section will be limited to only those records that match that (and all the other selected) checkbox value.

Example:
No boxes checked - 250 records returned
Box 1 checked - only those records where that box value is true returned - 100 records returned
Box 2 checked - only records where Box 1 & 2 are true returned - 20 records returned.
Box 5 checked - only records where Box 1 & 2 & 5 are true returned - 0 records returned
Box 2 unchecked - only records where Box 1 & 5 are true returned - 40 records returned
Box 8 checked - records where Box 1, 5, 8 iare true  returned - 3 records returned.

At this point, it is easy to figure out which record I want to look at and can click on it to show a detail screen.
What I am unable to figure out is how to pass the checked/unchecked value to my controller class (presumably in the onChange method)

Amit Chaudhary 8 - I am not sure if a wrapper class will work in this scenario because the checkboxes are not in the table of returned results.  If I am mistaken, please help me understand how to do this.

KapilC - Yes, my custom object has all the boolean fields that are going to be associated with the checkboxes.  My plan is to dynamically build an SOQL WHERE clause using those boolean values by passing to a build function a code saying which field should be included (see my original question for how I'm planning this).
Shrikant BagalShrikant Bagal
Hello Roger,

You can use "<apex:selectCheckboxes>" and <apex:actionSupport>
Please look at following controllr and VF
 
public class CheckBoxFilter{
	public List<String> lstSelectedValue { get; set;}
	
	//Constructor
	public CheckBoxFilter()
	{
		 lstSelectedValue = new List<String>();
	}
	
	// your checkbox values
	public List<SelectOption> getFilterValues(){
        	List<SelectOption> lstFilter = new List<SelectOption>();
        	lstFilter.add(new Selectoption('Box1','Box1'));
        	lstFilter.add(new Selectoption('Box2','Box1'));
        	lstFilter.add(new Selectoption('Box3','Box1'));
        	lstFilter.add(new Selectoption('Box4','Box1'));
        	return lstFilter;
   	 }
	
	public List<Your_Result_Object> getFilteredRecords(){
		//You will get all selected values in "lstSelectedValue"

		System.debug('mapFileter : ' + lstSelectedValue);
		//Your record Fetching logic
		
	}

}

VF
 
<apex:page controller="customController"/>
<apex:form>
<apex:actionStatus id="rstatus">
<apex:facet name="start">
    Processing...................
</apex:facet>
<apex:facet name="stop">
    Search Filter...................
</apex:facet>
</apex:actionStatus>

<apex:selectCheckboxes value="{!lstSelectedValue}" legendText="Filters" onselect="filter()">
    <apex:SelectOptions value="{!FilterValues}"></apex:SelectOptions>
    <apex:actionSupport event="onchange" action="{!getFilteredRecords}" status="rstatus" reRender="records" />
</apex:selectCheckboxes>

<apex:outputPanel id="records">
    <!--------- Your Record view ----------->
</apex:outputPanel>
</apex:form>
</apex:page>

if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
​Thanks! 

 
Roger PavelleRoger Pavelle
Thanks for the tip.  I'll try it out next week (since I'm away the rest of this one.
Just so I understand this better in the future, how is the checked value being passed from the VF code to the controller?  Is it the rstatus variable?  Is it possible to use this variable inside the action statement.  In other words, would it work if line 14 read:
14     <apex:actionSupport event="onchange" action="{!getFilteredRecords(rstatus)}"  reRender="records" />

Also, I don't see see anything in the suggested code that would remove a filter if the box is unchecked (which is why I want need to pass the value).
Roger PavelleRoger Pavelle
I ended up solving this problem using brute force.  I created a list that would hold a boolean value and passed the check box number to that function as a index.  I later build the indicated where clause by looping through the list and adding values when the box value is still true.

Here's the final code:
Apex:
                        <apex:inputCheckbox label="Found in S.W. Pennsylvania" id="regionFoundSWPAChk" >
                            <apex:actionSupport action="{!setChkBoxValue}" event="onchange" rerender="bolete_list">
                                <apex:param name="boxNum" value="49"/>
                            </apex:actionSupport>
                        </apex:inputCheckbox>

VF:
    private List<Boolean> chkBoxValues = new List<Boolean>{false,false,false,false,false,false,false,false,false,false,false,
        false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
        false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
        false,false,false,false,false,false,false,false,false,false,false,false,false,false};

    public void setChkBoxValue()
    {
        integer boxNum = integer.valueOf(system.currentPageReference().getParameters().get('boxNum'));
        boolean before = chkBoxValues[boxNum];
        chkBoxValues[boxNum] = !chkBoxValues[boxNum];
        System.debug('boxNum:' + boxNum + ' before:' + before);
        System.debug(currentChkBoxValues);
    }

    private void buildWhereClause()
    {
        Boolean exists = false;
        whereClause = '';
       
        //The first four values are for edibility, which are not mutually exclusive, so they are built
        // using or instead of and
        for (Integer i = 0; i<4; i++)
        {
            if (chkBoxValues[i])
            {
                if (exists)
                {
                    whereClause = whereClause + ' or (Edibility__c = ' + whereClauseValues[i] + ')';
                }
                else
                {
                    whereClause = 'Where ((Edibility__c = ' + whereClauseValues[i] + ')';
                    exists = true;
                }
            }
        }
        if (exists)
        {
            whereClause = whereClause + ') ';
        }
       
        //All the other values are individual values, so they are built with AND
        for (Integer i = 4; i<whereClauseValues.size(); i++)
        {
            if (chkBoxValues[i])
            {
                if (exists)
                {
                    whereClause = whereClause + 'and ' + whereClauseValues[i] + ' = true ';
                }
                else
                {
                    whereClause = 'Where ' + whereClauseValues[i] + ' = true ';
                    exists = true;
                }
            }           
        }
    }
 
This was selected as the best answer