2

Second day of being a coder and I'm getting this error...

line 1:70 no viable alternative at character '"' Error is in expression '{!search}' in component apex:commandButton in page unopenedcasespage: Class.UnopenedCases.search: line 6, column 1

An unexpected error has occurred. Your development organization has been notified.

Here's the code...

Apex Class

public with sharing class UnopenedCases {
        public list <Case> cas {get;set;}
        public String searchKey {get;set;}
        public void search(){
            string searchquery='SELECT parentId.caseNumber, parentId.subject FROM case WHERE status = "0"';
            cas= Database.query(searchquery);
        }
        public void clear(){
            cas.clear();
        }
    }

VF Page

<apex:page controller="UnopenedCases">
    <apex:form>
        <apex:commandButton value="Clear records" action="{!clear}"/>
        <apex:commandButton value="Search records" action="{!search}"/>
        <apex:pageBlock title="Search Result">
                <apex:pageBlockTable value="{!cas}" var="c">
                <apex:column value="{!c.subject}"/>
                <apex:column value="{!c.caseNumber}"/>
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I've tried unticking smart quotation marks on my mac and rewriting the whole thing but nothing I've tried has done anything.

Any help would be greatly appreciated.

1 Answer 1

7

Double quotes " are not used for SOQL query to specify a filtered value in conditional expression. You can use single quotes with escaping \'. Take a look at Quoted String Escape Sequences

public void search(){
    String searchquery='SELECT parentId.caseNumber, parentId.subject FROM case WHERE status = \'0\'';
    cas= Database.query(searchquery);
}

Another option, to use variable binding

public void search(){
    String caseStatus = '0';
    String searchquery = 'SELECT parentId.caseNumber, parentId.subject FROM case WHERE status = :caseStatus';
    cas= Database.query(searchquery);
}

Moreover, I recommend you use static SOQL queries until you need to have them dynamic explicitly. Compile errors are earlier detected, than runtime exceptions.

public void search(){
    cas= [SELECT parentId.caseNumber, parentId.subject FROM case WHERE status = '0'];
}

You must log in to answer this question.

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