I have the following function to get the available roles:

public List<String> getConsulteeRoles(){
    List<String> str = new List<String>();
    List<SelectOption>roles = new List<SelectOption>();
    Schema.DescribeFieldResult fieldResult = Consultee__c.Role__c.getDescribe();
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
    for(Schema.PicklistEntry f : ple){
        roles.add(new SelectOption(f.getLabel(), f.getValue()));
    return str;

I will like to query my custom object where the Role__c contains any of the returned list in my above function. Is there a a way of doing this? i.e. :

[SELECT Id FROM RS_Related_Contact__c WHERE Role__c INCLUDES(: getConsulteeRoles())]

1 Answer 1


I believe you'll have to use dynamic SOQL as bind clauses (:varName) can't be used with INCLUDES e.g.

string soql = 'SELECT Id FROM RS_Related_Contact__c WHERE Role__c INCLUDES ( \''
  + String.join( getConsulteeRoles(), '\',\'' ) 
  + '\' )';


That will build up a query like

SELECT Id FROM RS_Related_Contact__c WHERE Role__c INCLUDES ('RoleA','RoleB','RoleC')

You must log in to answer this question.

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