I was searching for a method to find the count of selected multi-select picklist. My aim is to select multiple options for Known-Languages in a Drop down and to display the selected count in a Counter and reflect the changes of count based on selection from multi-select picklist using Insert-Update before trigger.

But couldn't able to find a direct one and was able to see some workaround way using a List and the split method using ';' and it worked well (Referred Link)

I have written my trigger as below and it worked fine with my test cases.

trigger AccountMultiselect on Account (before insert, before update) {
    for(Account acc:Trigger.new){
            List<String> Langs = acc.Languages_Known__c.split(';');
            acc.Counter__c     = Langs.size();
        } else {
            acc.Counter__c     = 0;


Can some one let me know

  1. Are there any direct methods to check the count instead of List and split ? This approach was suggested somewhere 2013 and just want to know any new methods available recently.

  2. Does the above code have any loopholes?

  • 1
    As a one-liner, for performance: acc.Counter__c = acc.Languages_Known__c == null? 0: acc.Languages_Known__c.split(';').size();, but there's nothing wrong with the trigger as is. – sfdcfox Jan 30 '17 at 2:56

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).

| improve this answer | |
  • 1
    2, 5, and 6 will not occur in Trigger.new, though. There's no practical point in testing those, but otherwise a good answer. – sfdcfox Jan 30 '17 at 2:54

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)
| 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.