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
srav gowthamsrav gowtham 

i am getting error after executing plz help me

this is apex page:

public class DynamicSoql {
    public List<Account> accs {set;get;}
    public string accName {set;get;}
    public string accIndustry {set;get;}
    public void Search(){
        accs=[select name,industry from Account where name=:accName and industry=:accIndustry];
    }
     public void dynamicSearch(){
        string query='select id,name,industry from Account';
        if((accName!=null && accName!='') && (accIndustry!='' && accIndustry!=null)){
            query=query+'where name=\''+accName+'\' and industry=\''+accIndustry+'\''; 
        }else{
            if((accName!=null && accName!='')){
                query=query+'where name=\''+accName+'\'';
            }else{
                if((accIndustry!='' && accIndustry!=null)){
                    query=query+'where industry=\''+accIndustry+'\'';
                }
            }
        }
       accs=Database.query(query);     
    }
  
    
  }


This is VF page

<apex:page controller="DynamicSoql">
    <apex:form>
        <apex:pageBlock title="AdyInformation">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="search" action="{!Search}" />
               <apex:commandButton value="DynamicQuery" action="{!dynamicSearch}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel>EnterName</apex:outputLabel>
                    <apex:inputText value="{!accName}" />
                        </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel>EnterIndustry</apex:outputLabel>
                    <apex:inputText value="{!accIndustry}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock title="Success" rendered="{! !ISNULL(accs)}">
            <apex:pageBlockTable value="{!accs}" var="a">
                <apex:column value="{!a.name}" />
                <apex:column value="{!a.industry}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

         My Error is 
System.QueryException: unexpected token: =
Error is in expression '{!dynamicSearch}' in component <apex:commandButton> in page dynamicsoql: Class.DynamicSoql.dynamicSearch: line 21, column 1
Class.DynamicSoql.dynamicSearch: line 21, column 1
SFDC RohitSFDC Rohit
No space b/w from account and where condition
Om PrakashOm Prakash
Hi Srav,
Here is updated code which will work for your requirement.
Note:    Line number 11, 14 and 17 are modified.
query+'where  is modified with query+' where
 
public class DynamicSoql {
    public List<Account> accs {set;get;}
    public string accName {set;get;}
    public string accIndustry {set;get;}
    public void Search(){
        accs=[select name,industry from Account where name=:accName and industry=:accIndustry];
    }
     public void dynamicSearch(){
        string query='select id,name,industry from Account';
        if((accName!=null && accName!='') && (accIndustry!='' && accIndustry!=null)){
            query=query+' where name=\''+accName+'\' and industry=\''+accIndustry+'\''; 
        }else{
            if((accName!=null && accName!='')){
                query=query+' where name=\''+accName+'\'';
            }else{
                if((accIndustry!='' && accIndustry!=null)){
                    query=query+' where industry=\''+accIndustry+'\'';
                }
            }
        }
       accs=Database.query(query);     
    }
  
    
  }

 
srav gowthamsrav gowtham
thank you om prakash
Om PrakashOm Prakash
Thnaks for update.
Please mark the answer as best answer if worked for you so that this question will automatically mark solved and helpful for other members.