function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DarrellDDarrellD 

Querying a Multi Select Picklist Using Includes

Seen the postings related to this but cant find this use case.  Writing a class with an If...Statement to check if a String variable "DayOfWeek" is present in a multi select picklist.  I'm getting an error right after the variable 'calmonth' stating it wants a right parentheses.

 

Ex: DayOfWeek = 'Monday'

Multi Select Picklist = 'Monday';'Wednesday';'Friday'

Return = True since Monday is in list

 

Code:

String calday = c.ONE__DayofWeek__c;

String testing = [select ONE__Scheduled_Days__c from ONE__Schedule_Development__c where Id = :s].ONE__Scheduled_Days__c;**

**I tried putting this directly in If...Statement and using it as a variable

Problem Line In Code

If (calday includes (:testing)){....

 

Thanks,

Darrell

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Includes is only for a query. In regular code, you need something like this:

 

if( !String.isBlank( calday ) && 
    ( new Set< String >( calday.split(';') ) ).contains('Monday') ) {
// yes, calday has Monday...
}

Or, if you're sure nobody is going to mess with your picklist values, and you know that Monday won't appear elsewhere in the list (e.g. Monday Afternoon, Monday Morning), then you can use the more direct indexOf:

 

if( !String.isBlank( calday ) && calday.indexOf('Monday') > -1 ) {
// We found Monday!
}

 

All Answers

sfdcfoxsfdcfox

Includes is only for a query. In regular code, you need something like this:

 

if( !String.isBlank( calday ) && 
    ( new Set< String >( calday.split(';') ) ).contains('Monday') ) {
// yes, calday has Monday...
}

Or, if you're sure nobody is going to mess with your picklist values, and you know that Monday won't appear elsewhere in the list (e.g. Monday Afternoon, Monday Morning), then you can use the more direct indexOf:

 

if( !String.isBlank( calday ) && calday.indexOf('Monday') > -1 ) {
// We found Monday!
}

 

This was selected as the best answer
DarrellDDarrellD

**bleep**! Let me toil for a few hours yesterday on this!!  I used the .contains method which worked right away!

 

Darrell