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
SDFC FirstLevelerSDFC FirstLeveler 

Dynamic search in account

Hi experts,

I am trying to perform the dynamic search in account object but getting error while I input in respective fields: 

"System.QueryException: unexpected token: =
Error is in expression '{!desiredSearch}' in component <apex:commandButton> in page happysearchpage: Class.HappySearch.desiredSearch: line 18, column 1"
Class.HappySearch.desiredSearch: line 18, column 1

Please review my code and suggest me solution. 
Thanks

My Controller Code:
public class HappySearch {
    public list <Account> a {set;get;}
    public string aName     {set;get;}
    public string aType     {set;get;}
    
    public void desiredSearch(){
                        string Dsearch='Select Name, Type From Account';
                        if ((aName!=Null && aName!= '') && (aType!=''&& aType!=Null )){
                            Dsearch= Dsearch+ 'where name=\' '+aName+' \' and type=\' '+aType+' \'';}
                         else {
                            if(aName!=Null && aName!= ''){
                                    Dsearch= Dsearch+ 'where name=\' '+aName+' \'';} 
                                   else{
                                     if(aType!=Null && aType!=''){
                                         Dsearch= Dsearch+ 'where type=\' '+aType+' \'';}
                                       }
                                }
                          a= Database.query(Dsearch);
                        } 
                }

VF Page Code:
<apex:page controller="HappySearch" >
<apex:form >
    
    <apex:pageBlock title="Search">
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Name"/>
                <apex:inputText value="{!aName}"/>  
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Type"/>
                <apex:inputText value="{!aType}"/>  
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection> 
    
       <apex:pageBlockButtons location="Bottom">
           <apex:commandButton value="DSearch" Action="{!desiredSearch}"/>
       </apex:pageBlockButtons>
    </apex:pageBlock>
       
    <apex:pageBlock title="Search Result" rendered="(! !ISNULL(a))" >
       <apex:pageBlockTable value="{!a}" var="b">
       <apex:column value="{!b.Name}"/>
       <apex:column Value="{!b.Type}"/>
       </apex:pageBlockTable>    
    </apex:pageBlock>
     
</apex:form>
</apex:page>

P.S. it worked well if no inputs given to repective fields.   ​

 
Best Answer chosen by SDFC FirstLeveler
devedeve
Hi,
Their is some issue in dynamic query related to spaces and one issue in rendered condition of page block on vf page.Please try this code

Controller code:

public class HappySearch {
    public list <Account> a {set;get;}
    public string aName     {set;get;}
    public string aType     {set;get;}
    
    public void desiredSearch(){
                        string Dsearch='Select Name, Type From Account';
                        if ((aName!=Null && aName!= '') && (aType!=''&& aType!=Null )){
                            Dsearch= Dsearch+ ' where Name=\''+aName+'\' and Type=\''+aType+'\'';}
                         else {
                            if(aName!=Null && aName!= ''){
                                    Dsearch= Dsearch+ ' where Name=\''+aName+'\'';}
                                   else{
                                     if(aType!=Null && aType!=''){
                                         Dsearch= Dsearch+ ' Where Type=\''+aType+'\'';}
                                       }
                                }
                          a= Database.query(Dsearch);
                        }
                }


VF Page Code:

<apex:page controller="HappySearch" >
<apex:form >
    
    <apex:pageBlock title="Search">
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Name"/>
                <apex:inputText value="{!aName}"/>  
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Type"/>
                <apex:inputText value="{!aType}"/>  
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    
       <apex:pageBlockButtons location="Bottom">
           <apex:commandButton value="DSearch" Action="{!desiredSearch}"/>
       </apex:pageBlockButtons>
    </apex:pageBlock>
       
    <apex:pageBlock title="Search Result" rendered="{!a!=null}" >
       <apex:pageBlockTable value="{!a}" var="b">
       <apex:column value="{!b.Name}"/>
       <apex:column Value="{!b.Type}"/>
       </apex:pageBlockTable>    
    </apex:pageBlock>
     
</apex:form>
</apex:page>

 

All Answers

Ahmad J. KoubeissyAhmad J. Koubeissy
on the DSearch variable, try to add a space at the end of the query, I mean after account. so it becomes like this :
 
string Dsearch='Select Name, Type From Account ';

Dont forget to mark as best answer.
devedeve
Hi,
Their is some issue in dynamic query related to spaces and one issue in rendered condition of page block on vf page.Please try this code

Controller code:

public class HappySearch {
    public list <Account> a {set;get;}
    public string aName     {set;get;}
    public string aType     {set;get;}
    
    public void desiredSearch(){
                        string Dsearch='Select Name, Type From Account';
                        if ((aName!=Null && aName!= '') && (aType!=''&& aType!=Null )){
                            Dsearch= Dsearch+ ' where Name=\''+aName+'\' and Type=\''+aType+'\'';}
                         else {
                            if(aName!=Null && aName!= ''){
                                    Dsearch= Dsearch+ ' where Name=\''+aName+'\'';}
                                   else{
                                     if(aType!=Null && aType!=''){
                                         Dsearch= Dsearch+ ' Where Type=\''+aType+'\'';}
                                       }
                                }
                          a= Database.query(Dsearch);
                        }
                }


VF Page Code:

<apex:page controller="HappySearch" >
<apex:form >
    
    <apex:pageBlock title="Search">
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Name"/>
                <apex:inputText value="{!aName}"/>  
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel Value="Type"/>
                <apex:inputText value="{!aType}"/>  
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    
       <apex:pageBlockButtons location="Bottom">
           <apex:commandButton value="DSearch" Action="{!desiredSearch}"/>
       </apex:pageBlockButtons>
    </apex:pageBlock>
       
    <apex:pageBlock title="Search Result" rendered="{!a!=null}" >
       <apex:pageBlockTable value="{!a}" var="b">
       <apex:column value="{!b.Name}"/>
       <apex:column Value="{!b.Type}"/>
       </apex:pageBlockTable>    
    </apex:pageBlock>
     
</apex:form>
</apex:page>

 
This was selected as the best answer
SDFC FirstLevelerSDFC FirstLeveler
Still not worked