1

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.

1 Answer 1

2

Updated

You can create set<String> and pass into method like this

Set<String>() tempWhereList  = new Set<String>();  
tempWhereList.add('sample');
tempWhereList.add('sample2');
tempWhereList.add('sample3');

And you class will be

public class commonFunction {
  public static String whereInQueryString(String WhereField, Set<String> tempWhereList) {

    String QueryString = 'Select Id,Name FROM Contact WHERE ' + WhereField + ' IN : tempWhereList ';
    return QueryString;
  }
}

Now in call you method like this

Set<String>() tempWhereList  = new Set<String>();  
tempWhereList.add('sample');
tempWhereList.add('sample2');
tempWhereList.add('sample3');
String query = commonFunction.whereInQueryString('FirstName', tempWhereList  );
System.debug('query : ' + query);

Edit

Else return List of contacts.

public class commonFunction {
  public static List<contact> 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 Database.query(QueryString);
  }
}
5
  • Thanks for your help but I try to run this code and still it doesn't work.
    – Hope
    Commented Feb 17, 2016 at 5:28
  • @Hope check my updated ans
    – Ratan Paul
    Commented Feb 17, 2016 at 5:30
  • This is what I did with my code in the first place. But, I am trying to change it to process those data inside my other function for some reason. Is it possible?
    – Hope
    Commented Feb 17, 2016 at 5:40
  • @Hope then do one thing Query inside that method and return the list
    – Ratan Paul
    Commented Feb 17, 2016 at 5:43
  • 1
    I believe this is a good idea to return the database.query instead of String. Thank you so much for help. +
    – Hope
    Commented Feb 17, 2016 at 6:28

You must log in to answer this question.

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