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())]

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')
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.