1

I need to query using fieldset. I used the code below

string media='TV';
this.fpTV=(ABC__c)controller.getRecord();  // Using standard controller
String queryFields = '';
for(Schema.FieldSetMember f : SObjectType.xxx__c.FieldSets.TelevisionFieldSet.getFields()) {
 if(queryFields.length() > 0) {
  queryFields += ', ';
 }
queryFields += f.getFieldPath();
}
string whereClause='';
whereClause += 'ABC__c =: '+ fpTV.Id;
whereClause += ' AND YYY__c =: '+ media ;
//Query the records you want to display
this.fpTvList = Database.query('SELECT ' + queryFields +' XYZ__c ' + ' WHERE ' + whereClause);
}

But i received queryexception ' = '. Just to check , i removed the id check from where clause and tried. This time i received " variable does not exist:TV"

Pleasse help me out with the string query

1 Answer 1

2

First I would not use a ID as variable in the query but put it as a string:

whereClause += ' ABC__c = \'' + fpTV.Id + '\' ';

Then the same for the TV variable:

whereClause += ' AND YYY__c = \'' + media + '\' ';

Another thing I would check is the need of the extra comma before XYZ__c field in the query:

this.fpTvList = Database.query('SELECT ' + queryFields + ', XYZ__c ' + ' WHERE ' + whereClause);
2
  • Received this error " System.QueryException: Only variable references are allowed in dynamic SOQL/SOSL. " when i used whereClause += ' Media_Channel__c =: \''+ media + '\' '; Commented Mar 11, 2015 at 11:18
  • @SFDCLearner Don't use =: It is only allowed for the variable binding. Use whereClause += ' Media_Channel__c = \''+ media + '\' '; Commented Mar 11, 2015 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .