Good Day everyone, I have a problem in regards with calling a method that contain a dynamic SOQL. To explain further, below is a sample code:
public class commonFunction {
public static String whereInQueryString(String WhereField) {
Set<String> tempWhereList = new Set<String>();
// process some data here
tempWhereList.add('sample');
tempWhereList.add('sample2');
tempWhereList.add('sample3');
QueryString = 'Select Id,Name FROM Contact WHERE ' + WhereField + ' IN '+ tempWhereList;
return QueryString;
}
}
After creating the above code, I call it from the other class.
String query = commonFunction.whereInQueryString('FirstName');
System.debug('query : ' + query);
The problem is that I encounter the error
expecting a colon, found '{'
I've tried to update the code to something like this
QueryString = 'Select Id,Name FROM Contact WHERE ' + WhereField + ' IN :tempWhereList';
But since, the tempWhereList
is local variable I encounter error
Variable does not exist: tempWhereList
The given code is just a sample. I just want to ask what is the best thing to do. Please help. Thank you.