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
Nagashree Suresh 1Nagashree Suresh 1 

System.QueryException: unexpected token: = Error is in expression '{!getdata}' in component <apex:commandButton> in page soql_1: Class.SOQL_1.getdata: line 22, column 1

Hi Guys,

I am getting the error System.QueryException when executing below code can some one help:
Apex class:
public class SOQL_1 {
    public String name {set;get;}
    public String industry {set;get;}
    public list<Account> accounts {set;get;}
    public string query {set;get;}
    
    public void getdata()
        
    {
        query='select name,industry from Account';
        if(name!=''&&industry!=''){
        query= query + 'where name=\''+name+'\' and industry=\''+industry+'\'';
        }else{
            if(name!=''){
                query=query+'where name=\''+name+'\'';
            }else{
                if(industry!=''){
                    query=query +'where industry=\''+industry+'\'';
                }
            }
        }
        accounts = Database.query(query);
    }

VF page:
<apex:page controller="SOQL_1">
    <apex:form>
        <apex:pageblock title="dynamic soql">
            <apex:pageblockbuttons location ="bottom">
                <apex:commandButton value="search" action ="{!getdata}" />
            </apex:pageblockbuttons>
            
            <apex:pageblocksection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="name"/>
                    <apex:inputText value="{!name}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                   <apex:outputLabel value="industry"/>
                    <apex:inputText value="{!industry}"/>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
        </apex:pageblock>
        <apex:pageblock >
            <apex:pageblocktable rendered ="{!!ISNULL(accounts)}" value="{!accounts}" var= "a">
                       <apex:column value="{!a.name}"/>   
                       <apex:column value="{!a.industry}"/>  
            </apex:pageblocktable>
        </apex:pageblock>
    </apex:form>
    
</apex:page>

Thanks in advance
Raj VakatiRaj Vakati
Its space issue in where clause .. use the below code

 
public class SOQL_1 {
    public String name {set;get;}
    public String industry {set;get;}
    public list<Account> accounts {set;get;}
    public string query {set;get;}
    
    public void getdata()
        
    {
        query='select name,industry from Account';
        if(name!=''&&industry!=''){
            query= query + ' where name=\''+name+'\' and industry=\''+industry+'\'';
        }else{
            if(name!=''){
                query=query+' where name=\''+name+'\'';
            }else{
                if(industry!=''){
                    query=query +' where industry=\''+industry+'\'';
                }
            }
        }
        System.debug('query'+query);
        accounts = Database.query(query);
    }
}

 
Prabhat Sharma 6Prabhat Sharma 6
Hi Nagashree Suresh 1,

You need to give space in all the three query lines before "where" clause. Use the below snippet 

 if(name!=''&&industry!=''){
        query= query + ' where name=\''+name+'\' and industry=\''+industry+'\'';
        }else{
            if(name!=''){
                query=query+' where name=\''+name+'\'';
            }else{
                if(industry!=''){
                    query=query +' where industry=\''+industry+'\'';
                }
            }
        }

Thanks.
Nagashree Suresh 1Nagashree Suresh 1
Thank you it's working now!