+ Start a Discussion
Roger PavelleRoger Pavelle 

System.QueryException: value of filter

I can't figure out why I am getting the following error:
System.QueryException: value of filter criterion for field 'Edibility__c' must be of type string and should be enclosed in quotes

The checkboxes call the setCheckBoxValue function, which is supposed to track whether the box is checked or not.  This array is later used by the buildWhereClause function to rerender a search results table based on the chekced values.  There are 61 checkboxes and all but the first 4 checkboxes work correctly.  Everything was working for a while, but then a change in the custom object led me to change the "value" field, but I later changed it back to what it used to be (even going so far as to copy the code from other boxes and changing the value).

Why doesn't this work anymore?


Apex code:
                <apex:inputCheckbox label="Choice" id="edibilityChoiceChk" >
                    <apex:actionSupport action="{!setChkBoxValue}" event="onchange" rerender="bolete_list">
                        <apex:param name="boxNum" value="0"/>
                    </apex:actionSupport>
                </apex:inputCheckbox>               

Controller code (selected pieces):
    private List<String> whereClauseValues = new List<String>{'4', '3', '2', '1', 'poreColorRed__c', 'poreColorBrown__c',
        'sporePrintYellow__c', 'sporePrintPink__c', 'sporePrintBrown__c'};

    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 = ' + string.escapeSingleQuotes(whereClauseValues[i]);
                }
                else
                {
                    whereClause = 'Where (Edibility__c = ' + string.escapeSingleQuotes(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;
                }
            }           
        }
    }
 
Best Answer chosen by Roger Pavelle
Roger PavelleRoger Pavelle
I fixed this a while ago but forgot to close out the question.  I think it was just a coding error on my part in terms of getting the number of parentheses correct.

final code for the first four boxes:
        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 Answers

Prabhat Kumar12Prabhat Kumar12
Change you filter query something like this.
 
//Change
whereClause = 'Where (Edibility__c = ' + string.escapeSingleQuotes(whereClauseValues[i]); 

//To 

whereClause = 'Where (Edibility__c IN :' + string.escapeSingleQuotes(whereClauseValues[i]);

 
Roger PavelleRoger Pavelle
I think the problem is that the setCheckBoxValue function isn't logging the value correctly.  I have test functions to show the values in the chkBoxValues and whereClause variables, and neither one shows the first four boxes being set.

Controller code to display values:
    public string currentChkBoxValues
    {
        get {
            return string.join(chkBoxValues, ', ');
        }
    }
    public string currentWhereClause
    {
        get {
            return whereClause;
        }
    }
 
Roger PavelleRoger Pavelle
I fixed this a while ago but forgot to close out the question.  I think it was just a coding error on my part in terms of getting the number of parentheses correct.

final code for the first four boxes:
        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 + ') ';
        }
 
This was selected as the best answer