Another Salesforce blog!!!

Salesforce, Apex

AD hoc soql query


Constructor:

public List<String> lst1Fields {set;get;}
public List<String> lst2Fields {set;get;}
public List<String> lst3Fields {set;get;}

List<custom_object__c> listCustomObject = [soql];

for(custom_object__c c: listCustomObject) {
  if(string.isEmpty(c.field__c)) {
     lst1Fields.add(c.field_name);   
  }
}


public PageReference searchNow(){

String qryStr = 'SELECT Id';
String conditionStr = ' WHERE Name_ID__c = \''+ searchWord +'\'' + ' OR Name_ID__c LIKE '+'\''+ searchWord +'\'';
 
for(String searchField : lst1Fields){
    qryStr += ',' + searchField;
}
qryStr +=' FROM Common__c' + conditionStr;
String qryStrForEvent = 'SELECT Id';
for(String searchField : lst2Fields){
   qryStrForEvent += ','+ searchField ;
}
qryStr2 +=' FROM mycustomobject__c ' + conditionStr;
lst = Database.query(qryStr2);
 
}

Leave a Reply

Your email address will not be published.

*