You need to sign in to do that
Don't have an account?
prasanth kumar
error at visualforce page for simple soql program
Visualforce ErrorHelp for this Page
System.QueryException: unexpected token: =
Error is in expression '{!getdata}' in component <apex:commandButton> in page dynamicsoql1: Class.dynamicsoql.getdata: line 32, column 1
Class.dynamicsoql.getdata: line 32, column 1
System.QueryException: unexpected token: =
Error is in expression '{!getdata}' in component <apex:commandButton> in page dynamicsoql1: Class.dynamicsoql.getdata: line 32, column 1
Class.dynamicsoql.getdata: line 32, column 1
public class dynamicsoql { public list<account> accs{set;get;} public string aname{set;get;} public string aindustry{set;get;} public string query{set;get;} public void getdata() { query='select id,name,industry from account'; if((aname!='' && aname!=null) && (aindustry!='' && aindustry!=null)) { query=query+'where Name=\''+aname+ '\' and Industry= \'' +aindustry+ '\''; } else { if (aname!='' && aname!=null) { query=query+'where Name=\'' +aname+ '\''; } if(aindustry!='' && aindustry!=null) { query=query+'where Industry=\''+aindustry+'\''; } } accs=Database.query(query); } }
<apex:page controller="dynamicsoql"> <apex:form > <apex:pageBlock title="record"> <apex:pageBlockSection > <apex:pageBlockSectionItem > <apex:outputLabel value="name"/> <apex:inputText value="{!aname}"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel value="industry"/> <apex:inputText value="{!aindustry}"/> </apex:pageBlockSectionItem> <apex:commandButton value="submit" action="{!getdata}" reRender="one" /> </apex:pageBlockSection> </apex:pageBlock> <apex:pageBlock id="one"> {!query} <apex:pageBlockTable value="{!accs}" var="a"> <apex:column value="{!a.name}"/> <apex:column value="{!a.industry}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>
you can use system.debug in your code to see what actually gets generated in the query string..
if I use the name as test, then the generated query is
there is a space missing between account and where which is causing the error. if you update your query initializaton with a space at the end , then it will fix the error.
By the way, if you are using dynamic soql alot, you may want to build a helper class/method to do this for you like
Public static string ConstructWhereClause(...) where you can pass in a list. map or individual parameters.
Thx
Problem was space only . Please check below post with dynamic search and pagination. I hope that will help u
http://amitsalesforce.blogspot.in/2015/04/pagination-using-standardsetcontroller.html
you can create the dynamic method like below with like opp
Please let us know if that will h elp u
Thanks
Amit Chaudhary