You need to sign in to do that
Don't have an account?
Nagashree Suresh 1
System.QueryException: unexpected token: = Error is in expression '{!getdata}' in component <apex:commandButton> in page soql_1: Class.SOQL_1.getdata: line 22, column 1
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
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
You need to give space in all the three query lines before "where" clause. Use the below snippet
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+'\'';
}
}
}
Thanks.