I'm trying to create a dynamic SOQL string based on the type of fields being used. If the field is a text area, it contains a list of comma delimited strings. For each text area field, I create a list of the delimited values, and use "IN :TAValues" in the dynamic SOQL.
Problem is, if there is more than one text area field, the values in 'TAValues' get overwritten by the last field evaluated in the loop. I can't use a map or TAValues[i] in the dynamic query - how could this be done?
public Map<String, String> CriteriaFieldNameToValue = new Map<String, String>();
String QString = 'SELECT ID, Name FROM Account WHERE ';
Map<String, Schema.SObjectField> fieldMap = ((SObject)Type.forName('Schema','Account_Rule__c').newInstance()).getSObjectType().getDescribe().fields.getMap();
for (String FieldName: CriteriaFieldNameToValue.keySet()){
String FieldValue = CriteriaFieldNameToValue.get(FieldName);
String FieldType = String.valueof(fieldMap.get(FieldName).getDescribe().getType());
system.debug(FieldName +' = '+FieldValue);
if(FieldType == 'TEXTAREA'){
String [] TAValues = FieldValue.split(',');
QString = QString + FieldName +' IN :TAValues AND ';
system.debug('QString = '+QString);
system.debug('TAValues = '+TAValues);
}
}
QString = QString.removeEndIgnoreCase(' AND ');
system.debug('QString = '+QString);
return Database.getQueryLocator(QString);