0

I'm trying to run a SOQL query that pulls campaigns where the value of a custom picklist field (Newsletter_Subscriptions__c) is included in a Contacts multi-select picklist fields (Newsletter_Subscriptions__c). Currently, I've dumped the Contact's picklist values into a list (contactSubscriptions) that I'm querying against but the query isn't pulling any campaigns. Any advice?

list subscriptionCampaigns = [SELECT ID FROM Campaign WHERE Newsetter_Subscriptions__c IN :contactSubscriptions];

1

If you are querying against a mulit-select picklist, you need to use includes in your query.

list subscriptionCampaigns = [
    SELECT ID FROM Campaign 
    WHERE Newsetter_Subscriptions__c INCLUDES :contactSubscriptions];

If Newsletter_Subscriptions__c is the multi-select and contactSubscriptions contains all of the values you want this should return any Campaign that has at least one selected. If you need to only select those that match all then you need to join the contactSubscriptions into a single semi-colon delimited string and have that as the only item on the list

There are some more examples in the Querying Multi-Select Picklist docs

  • If you need to find matches, is order guaranteed? – Adrian Larson Feb 22 '15 at 17:49
  • I think you mean that if you have INCLUDES ('A;B', 'C') will everything that matches 'A;B' appear in the returned list prior to everything that matches 'C'. I don't see anything in the documentation stating that, so I guess it is not guaranteed. – Peter Knolle Feb 22 '15 at 20:30
  • I was referencing "If you need to only select those that match all then you need to join the contactSubscriptions into a single semi-colon delimited string and have that as the only item on the list." Checking against "A;B;C" won't find "B;A;C". – Adrian Larson Feb 22 '15 at 20:31

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.