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
prasanth kumarprasanth kumar 

error at visualforce page for simple soql program

Visualforce ErrorHelp for this Page
System.QueryException: unexpected token: =
Error is in expression '{!getdata}' in component <apex:commandButton> in page dynamicsoql1: Class.dynamicsoql.getdata: line 32, column 1
Class.dynamicsoql.getdata: line 32, column 1

 
public class dynamicsoql 
{ 
    public list<account> accs{set;get;}  
    public string aname{set;get;}
    public string aindustry{set;get;}
    public string query{set;get;}
    
    public void getdata()
    {
     query='select id,name,industry from account';
        
        if((aname!='' && aname!=null) && (aindustry!='' && aindustry!=null)) 
        {
          query=query+'where Name=\''+aname+ '\' and Industry= \'' +aindustry+ '\'';
            
        }
        else
        {
            if (aname!='' && aname!=null) {
                
           query=query+'where Name=\'' +aname+ '\'';
           
            }
            
            
             if(aindustry!='' && aindustry!=null) {
           query=query+'where Industry=\''+aindustry+'\'';  
             
           }
      }
           
       accs=Database.query(query);
    }
    
}
 
<apex:page controller="dynamicsoql">
<apex:form >
    <apex:pageBlock title="record">
        <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="name"/>
            <apex:inputText value="{!aname}"/>
            </apex:pageBlockSectionItem>
             <apex:pageBlockSectionItem >
            <apex:outputLabel value="industry"/>
            <apex:inputText value="{!aindustry}"/>
            </apex:pageBlockSectionItem>
            <apex:commandButton value="submit" action="{!getdata}" reRender="one" />
        </apex:pageBlockSection>
    </apex:pageBlock>
          <apex:pageBlock id="one">
              {!query}

    <apex:pageBlockTable value="{!accs}" var="a">
        <apex:column value="{!a.name}"/>
        <apex:column value="{!a.industry}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
       </apex:form>
</apex:page>

 
Vamsi KrishnaVamsi Krishna
Prasanth

you can use system.debug in your code to see what actually gets generated in the query string..
system.debug(query);    
accs=Database.query(query);

if I use the name as test, then the generated query is 
select id,name,industry from accountwhere Name='test'

there is a space missing between account and where which is causing the error. if you update your query initializaton with a space at the end , then it will fix the error.
​query='select id,name,industry from account ';

 
William TranWilliam Tran
Adding that space should fix it.

By the way, if you are using dynamic soql alot, you may want to build a helper class/method to do this for you like
Public static string ConstructWhereClause(...) where you can pass in a list. map or individual parameters.

Thx
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
public class dynamicsoql 
{ 
    public list<account> accs{set;get;}  
    public string aname{set;get;}
    public string aindustry{set;get;}
    public string query{set;get;}
    
    public void getdata()
    {
     query='select id,name,industry from account ';
        
        if((aname!='' && aname!=null) && (aindustry!='' && aindustry!=null)) 
        {
          query=query+' where Name=\''+aname+ '\' and Industry= \'' +aindustry+ '\'';
            
        }
        else
        {
            if (aname!='' && aname!=null) 
            {
                query=query+' where Name=\'' +aname+ '\'';
            }
            if(aindustry!='' && aindustry!=null) 
            {
                query=query+' where Industry=\''+aindustry+'\'';  
            }
      }
           
       accs=Database.query(query);
    }
    
}

Problem was space only . Please check below post with dynamic search and pagination. I hope that will help u

http://amitsalesforce.blogspot.in/2015/04/pagination-using-standardsetcontroller.html

you can create the dynamic method like below with like opp
public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
           strFilter  = strFilter  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Phone like \''+acc.Phone+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
           }
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(2);
        }
        else
        {
        }
       return null;
    }

Please let us know if that will h elp u

Thanks
Amit Chaudhary