You need to sign in to do that
Don't have an account?
SDFC 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:
VF Page Code:
P.S. it worked well if no inputs given to repective fields.
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.
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
Dont forget to mark as best answer.
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>