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
ManjeetManjeet 

Plz help on Dynamic SOQL

Hi All

I want to build a criteria on the basis of search fields and then want to use this criteria in search method query .

 

 

  public String buildCriteria(){
    
        String crt = null ;
         customerName = Prospects.customer_name__c;
           if(customerName != null && customerName !='' )
               crt = ' customer_name__c like \'+customerName+\'';

           if(prospectRefNo != null && prospectRefNo != '')
               if(crt != null)
                    crt += ' and name = \'+prospectRefNo+\'' ;
                else
                    crt = ' name = \'+prospectRefNo+\'' ;
            
            
        return crt;
    }

 

 

These lines are in search method which call buildCriteria first and then execute the code

if(criteria != null )
                prospectsList = Database.query('select  name,customer_name__c,type_of_relationship__c,'+
               'prospect_source__c,e_mail__c from  prospects__c where ' + criteria);

 

But when I try to search Prospects i got an exception or the i dont get expected result..

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Manjeet,

Try this

 

String crt = null ;
         customerName = Prospects.customer_name__c;
           if(customerName != null && customerName !='' )
               crt = ' customer_name__c like \'' + customerName + '\'';

           if(prospectRefNo != null && prospectRefNo != '')
               if(crt != null)
                    crt += ' and name = \'' + prospectRefNo + '\'' ;
                else
                    crt += ' name = \'' + prospectRefNo + '\'' ;
            
            
        return crt;

 

 

if(criteria != null )

// Please add a debug statement to check the where Clause (criteria)
        String strQuery = 'select  name,customer_name__c,type_of_relationship__c,prospect_source__c,e_mail__c from  prospects__c where ' + criteria;
                prospectsList = Database.query(strQuery);
        
        return null;

All Answers

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Manjeet,

Try this

 

String crt = null ;
         customerName = Prospects.customer_name__c;
           if(customerName != null && customerName !='' )
               crt = ' customer_name__c like \'' + customerName + '\'';

           if(prospectRefNo != null && prospectRefNo != '')
               if(crt != null)
                    crt += ' and name = \'' + prospectRefNo + '\'' ;
                else
                    crt += ' name = \'' + prospectRefNo + '\'' ;
            
            
        return crt;

 

 

if(criteria != null )

// Please add a debug statement to check the where Clause (criteria)
        String strQuery = 'select  name,customer_name__c,type_of_relationship__c,prospect_source__c,e_mail__c from  prospects__c where ' + criteria;
                prospectsList = Database.query(strQuery);
        
        return null;

This was selected as the best answer
ManjeetManjeet

thanks biswas for your quick reply......