How to Find the Count of Multi-Select Picklist?
Is there any direct methods to check the count instead of List and Split method using ';'
Nope, splitting by semicolon and calling size()
on the resulting list is pretty much it. There are probably one or two other ways (1 + # of semicolons), but the method you're using now is likely the simplest.
Does the above code have any loop holes?
Probably not. It's a short piece of code that uses pretty common (and standard) methods.
Generally speaking, having a good suite of unit tests is important. Having them would increase one's confidence that a piece of code will behave as expected under a variety of circumstances.
If you don't have tests that verify the following cases, I'd suggest writing tests to cover these cases:
- What happens when
Languages_Known__c
is null? - What happens when
Languages_Known__c
is empty (but not null)? - Do you get a count of 1 when
Languages_Known__c
only selects one option (no semicolons)? - Do you get a count of 2 when
Languages_Known__c
selects two options? - What happens when
Languages_Known__c
only contains semicolons? - What happens when
Languages_Known__c
contains duplicate selections?
The second, fifth, and sixth cases shouldn't occur in normal use, but can happen if some other code is writing the value of Languages_Known__c
(which is just a semicolon-delimited string, after all).
If you don't have much values in your multi-picklist you could do it with a formula field(Number) :
IF(INCLUDES(Languages_Known__c,"ENGLISH"), 1 , 0) +
IF(INCLUDES(Languages_Known__c,"FRENCH"), 1 , 0) +
IF(INCLUDES(Languages_Known__c,"SPANISH"), 1 , 0)