You need to sign in to do that
Don't have an account?
Manjeet
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..
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
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;
thanks biswas for your quick reply......