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

Count the options selected from multiselect picklist

How to count number of options selected from multiselect picklist
Mahesh DMahesh D
Hi Manu,

Please check the following sample code:
trigger CountingValues on MyObject__c (before insert, before update) {

    if (Trigger.isUpdate) {
        for (MyObject__c mo : {
            if (mo.Colors__c != Trigger.oldMap.get(mo.Id).Colors__c) {
                if (String.isNotBlank(mo.Colors__c) {
                    List<String> colors = mo.Colors__c.split(';');
                    mo.ColorsCount__c = colors.size();
                } else {
                    mo.ColorsCount__c = 0;

If you want to implement it the formula then:
Create a new field on the opportunity object: 

Datetype = Formula 
Result Type = Number 
Formula = 

IF(INCLUDES( Multi_Picklist_1__c, "A"), 1,0) + 
IF(INCLUDES( Multi_Picklist_1__c, "B"), 1,0) + 
IF(INCLUDES( Multi_Picklist_1__c, "C"), 1,0) + 
IF(INCLUDES( Multi_Picklist_1__c, "D"), 1,0) + 
IF(INCLUDES( Multi_Picklist_1__c, "E"), 1,0) + 
IF(INCLUDES( Multi_Picklist_1__c, "F"), 1,0)

Also look into the below URL

Please let me know if it helps.

Thank you very much for your response. My requirement is to display a message on VF page when user selects more than 4 options from multiselect picklist. 

Sunil MadanaSunil Madana
Hi, below code should help you.
<apex:page standardController="Account" showHeader="true" sidebar="true" id="VF_Page" recordSetVar="accounts">
        var outStr = new Array( );
        function outputTxt(){
            var e = document.getElementById("VF_Page:VF_Form:VF_PageBlk:VF_PageBlksec1:VF_PageBlksecItem1:multipickStr");
            var selectedVal = e.options[e.selectedIndex].value;
            document.getElementById("VF_Page:VF_Form:VF_PageBlk:VF_PageBlksec1:VF_PageBlksecItem2:outputString").innerHTML += selectedVal + ",";
            outStr.push( selectedVal );
            if( outStr.length > 3) {
                document.getElementById("VF_Page:VF_Form:VF_PageBlk:VF_PageBlksec1:VF_PageBlksecItem3:arrayleng").innerHTML = "Total count exceed 4";
            } else {
                document.getElementById("VF_Page:VF_Form:VF_PageBlk:VF_PageBlksec1:VF_PageBlksecItem3:arrayleng").innerHTML = "";
    <apex:form id="VF_Form">
        <apex:pageBlock id="VF_PageBlk">
            <apex:pageBlockSection id="VF_PageBlksec1" title="Single Selection Picklist">
                <apex:pageBlockSectionItem id="VF_PageBlksecItem1">
                    <apex:inputField value="{!Account.CustomerPriority__c}" id="multipickStr" onchange="outputTxt();" />
                <apex:pageBlockSectionItem id="VF_PageBlksecItem2">
                    <apex:outputText id="outputString"></apex:outputText>
                <apex:pageBlockSectionItem id="VF_PageBlksecItem3">
                    <apex:outputText id="arrayleng" style="color:red; font-size:15px;"></apex:outputText>
            <apex:pageBlockSection id="VF_PageBlksec2" title="Multi Selection Picklist">
                <apex:pageBlockSectionItem id="VF_PageBlksecItem2">                    
                    <apex:selectList multiselect="true" size="7" onclick="outputTxt();">
                        <apex:selectOption itemValue="INDIA" itemLabel="India"/>
                        <apex:selectOption itemValue="USSR" itemLabel="USSR"/>
                        <apex:selectOption itemValue="United Kingdom" itemLabel="UK"/>
                        <apex:selectOption itemValue="United States" itemLabel="US"/>
                        <apex:selectOption itemValue="Malaysia" itemLabel="Malaysia"/>
Let us know if it helps. Thanks.
Mohd Zeeshan 3Mohd Zeeshan 3
trigger CountCounter on Account (before insert,before update) {
        for (Account Acc : {
            If(acc.Package__c !=null) {
                integer count = acc.Package__c.countMatches(';')+1;
                acc.Package_Choose__c = count;
            else {
                acc.Package_Choose__c = 0;
John GuerriereJohn Guerriere
Can you share your test class Mohd?

trigger CountCounter on Opportunity (before insert,before update) {
        for (Opportunity opp : {
            If(opp.POI__c !=null) {
                integer count = opp.POI__c.countMatches(';')+1;
                opp.PCount__c = count;
            else {
                opp.PCount__c = 0;
Megan Frost 18Megan Frost 18
Does anyone have a test class they are willing to share?
Nikhil Muppidi 2Nikhil Muppidi 2
I used to Trigger to count the options selected in the Account object Mobile filed which has 5 options.

Below is my trigger code:

trigger AccountMobileCounter on Account (before insert,before update) {
    List<String> mobilesSelected = new List<String>();
    Integer counter;
    for(Account acc:{
        String multiselect = acc.Phones__c;
            mobilesSelected  = multiselect.split(';');
            counter                = mobilesSelected.size();
            acc.Counter__c   = counter;
            acc.Counter__c = 0;

Hope this answer helps you.
Samantha Lisk 10Samantha Lisk 10
How does the semicolon work in the split method? What is it splitting?