You need to sign in to do that
Don't have an account?
SOQL Includes on multi-selects
Hi All,
I want to select records where the any of the values of two multi-selects match.
I am using includes which I think is the only way, but unless both multi-selects match, it doesn't return any values.
Any ideas?
Thanks
List<Account> fromLead = [select Id, Name, BillingCity, Country_ISO_Code__c, Type, Accreditations__c from Account where Type = 'Dealer' AND Country_ISO_Code__c =: lead.Country_ISO__c AND Accreditations__c includes (: lead.All_Accreditation_Endorsements__c ) AND Accreditations__c != null LIMIT 1000]; accs = fromLead;
You are absolutely correct. You need to do something like the following:
It's not nice to read, but it gets the job done. Your milage may vary, and you'll definitely want to kick the tires before you give it a serious lap around the track.
All Answers
This is expected behavior. When you choose multiple values from a picklist, it gets represented as "Value 1;Value 2". If you consider your original query:
When you literally substitute a multiselect value in place, it gets translated to:
If you check the syntax for filtering multi-select picklists, you can use the following syntax:
= Matches the string
!= Does not match the string
includes Contains the specified string
excludes Does not contain the specified string
; Specifies "and" for two or more strings.
As you can see, then, the translation becomes an "AND" value for "Value 1" and "Value 2". To use "OR", the string would have to appear as follows:
To get the bind variable to look just like that, you would have to be able to use a Set or List. However, at the time of this posting, that syntax appears to be supported, based on my tests. It appears you'll need to use Dynamic Apex and create a query string that resembles the statement above. You'll need to a) split the string at the delimiter, b) build a comma-delimited value list, and c) place that into your query string.
Thanks for response sfdcFox again - i will try that.
Hello. I'm trying to do the same thing, and actually tried to do it the same way you're suggesting. No luck so far.
When I debug my code, it shows the variable being used in the includes statement is formatted correctly 'Value1','Value 2' etc. But it returns no results.
When I make sure the string has only one value, formatted 'Value 1', it still doesn't work.
But, if I make sure the string has only one value and no apostrophe Value 1, it will return results.
I thought maybe it was automatically putting in the apostrophes in front and back of the string, so a tried formatting Value 1','Value 2 - that didn't work either. It appears that anything inside a string variable will be treated as one string, ignoring the commas and apostrophes. :(
You are absolutely correct. You need to do something like the following:
It's not nice to read, but it gets the job done. Your milage may vary, and you'll definitely want to kick the tires before you give it a serious lap around the track.
Then once you have the string built out, you then pass it into dynamic SOQ like below
Good luck
Thanks to both of you!! It does hurt to read at first, but once you catch up it's very cool! That'll work.
Thanks! That helped!
cool