• srav gowtham
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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
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