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){
if(String.isNotBlank(acc.Languages_Known__c)){
List<String> Langs = acc.Languages_Known__c.split(';');
acc.Counter__c = Langs.size();
} else {
acc.Counter__c = 0;
}
}
}
Can some one let me know
Are there any direct methods to check the count instead of
List
andsplit
? This approach was suggested somewhere 2013 and just want to know any new methods available recently.Does the above code have any loopholes?
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.