You need to sign in to do that
Don't have an account?

how to search a specific record by enter a keyword in search dialog box
I want to search a specific account record when i enter a record name and click search button? i was written the following code. While compliling the code there is no errors but when i click search button in visualforce page the following query exception throws.
System.QueryException: unexpected token: 'Name'
Error is in expression '{!queryData}' in component <apex:commandButton> in page soqldynamic: Class.DynamicSOQL.queryData: line 24, column 1
Class.DynamicSOQL.queryData: line 24, column 1
my class:
public class DynamicSOQL {
public string accName{set;get;}
public string accIndustry{set;get;}
public List<Account>a{set;get;}
public void queryData(){
if(a!=null)
a.clear();
string q='select name,Industry from Account WHERE Name=:accName';
if(accName!=''&&accName!=null&&accIndustry!=''&&accIndustry!=null){
q=q+'where Name=\''+accName+'\'and Industry=\''+accIndustry+'\'';
}
else {
if(accName!=''&&accName!=null){
q=q+'where Name=\''+accName+'\'';
}
else{
if(accIndustry!=''&&accIndustry!=null){
q=q+'where industry=\''+accIndustry+'\'';
}
}
}
a=database.query(q);
}
}
visualforce page:
<apex:page controller="DynamicSOQL" showHeader="true" sidebar="false">
<apex:form >
<apex:pageBlock title="Account Data">
<apex:pageBlockButtons location="top">
<apex:commandButton value="submit" action="{!queryData}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
Enter Name: <apex:inputText title="Enter Name" value="{!accName}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Enter Industry:<apex:inputText title="Enter Industry" value="{!accIndustry}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Account Records" rendered="{! !ISNULL(a)}">
<apex:pageBlockTable value="{!a}" var="b">
<apex:column value="{!b.Name}"/>
<apex:column value="{!b.industry}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
can any one help me what is the mistake that i have made in the code
System.QueryException: unexpected token: 'Name'
Error is in expression '{!queryData}' in component <apex:commandButton> in page soqldynamic: Class.DynamicSOQL.queryData: line 24, column 1
Class.DynamicSOQL.queryData: line 24, column 1
my class:
public class DynamicSOQL {
public string accName{set;get;}
public string accIndustry{set;get;}
public List<Account>a{set;get;}
public void queryData(){
if(a!=null)
a.clear();
string q='select name,Industry from Account WHERE Name=:accName';
if(accName!=''&&accName!=null&&accIndustry!=''&&accIndustry!=null){
q=q+'where Name=\''+accName+'\'and Industry=\''+accIndustry+'\'';
}
else {
if(accName!=''&&accName!=null){
q=q+'where Name=\''+accName+'\'';
}
else{
if(accIndustry!=''&&accIndustry!=null){
q=q+'where industry=\''+accIndustry+'\'';
}
}
}
a=database.query(q);
}
}
visualforce page:
<apex:page controller="DynamicSOQL" showHeader="true" sidebar="false">
<apex:form >
<apex:pageBlock title="Account Data">
<apex:pageBlockButtons location="top">
<apex:commandButton value="submit" action="{!queryData}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
Enter Name: <apex:inputText title="Enter Name" value="{!accName}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Enter Industry:<apex:inputText title="Enter Industry" value="{!accIndustry}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Account Records" rendered="{! !ISNULL(a)}">
<apex:pageBlockTable value="{!a}" var="b">
<apex:column value="{!b.Name}"/>
<apex:column value="{!b.industry}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
can any one help me what is the mistake that i have made in the code
try this onces i only corraction in first if block where both are not null
now it will work :)
Thanks
Mark it best answer if it helps you :)
All Answers
try this onces i only corraction in first if block where both are not null
now it will work :)
Thanks
Mark it best answer if it helps you :)