function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VempallyVempally 

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...
Best Answer chosen by Vempally
SFDC_DevloperSFDC_Devloper
Hi,

try below code..

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;  
         }

Thanks,
Rockzz

All Answers

SFDC_DevloperSFDC_Devloper
Hi,

try below code..

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;  
         }

Thanks,
Rockzz
This was selected as the best answer
asish1989asish1989
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;
         }
Sure@DreamSure@Dream
Hi Suresh,


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