• Nagashree Suresh 1
  • NEWBIE
  • 0 Points
  • Member since 2018

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