You need to sign in to do that
Don't have an account?
Vempally
Issue with Dynamic SOQL
Hi everyone the folowing code gives an error
System.QueryException: unexpected token: 'Product_Category__c'
public List<Product__c> getsearchproduct(){
List<Product__c> searchlist = new List<Product__c>();
string qry ='select Product_Name__c,Price__c,Product_Image__c, Company__c from Product__C where Product_Department__c =: selecteddept';
if(selectedcat != 'none')
{
qry+= 'AND Product_Category__c=: selectedcat';
}
if(choosegender!= 'none')
{
qry+= 'AND Trending_For__c =: choosegender';
}
searchlist = database.query(qry);
return searchlist;
}
need help to sort out...
System.QueryException: unexpected token: 'Product_Category__c'
public List<Product__c> getsearchproduct(){
List<Product__c> searchlist = new List<Product__c>();
string qry ='select Product_Name__c,Price__c,Product_Image__c, Company__c from Product__C where Product_Department__c =: selecteddept';
if(selectedcat != 'none')
{
qry+= 'AND Product_Category__c=: selectedcat';
}
if(choosegender!= 'none')
{
qry+= 'AND Trending_For__c =: choosegender';
}
searchlist = database.query(qry);
return searchlist;
}
need help to sort out...
try below code..
Thanks,
Rockzz
All Answers
try below code..
Thanks,
Rockzz
List<Product__c> searchlist = new List<Product__c>();
string qry ='select Product_Name__c,Price__c,Product_Image__c, Company__c from Product__C where Product_Department__c =: selecteddept';
if(selectedcat != 'none')
{
qry+= 'AND Product_Category__c=: \''+selectedcat;
}
if(choosegender!= 'none')
{
qry+= 'AND Trending_For__c =: \''+choosegender;
}
searchlist = database.query(qry);
return searchlist;
}
There should be space after the ' in the below line:
qry+= 'AND Product_Category__c=: selectedcat' should be changed to qry+= ' AND Product_Category__c=: selectedcat';
There should be proper spaces, while building the dynamic queries.
Thanks