Search This Blog

Sunday, November 1, 2015

How to assign values to multiselect picklist field in apex

In the example below Alphabets__c is a multiselect picklist field in Account object.
In order to set values to multi-select picklist ,first we need to form a string with
each selected value followed by ";" and then assign this string to multiselect picklist field.
Apex Class :
public class MutlipicklistFieldValue {
    public MutlipicklistFieldValue () {
    }
    public void insertAccount() {
        Account newAccount = new Account();
        newAccount.name = 'Testing Multipicklist Value';
        string values = 'A;B;D';
        newAccount.Alphabets__c = values;
        insert newAccount;
        system.debug('newAccountnewAccount'+newAccount);
    }
}
Visualforce Page :
<apex:page controller="MutlipicklistFieldValue" action="{!insertAccount}">
</apex:page>

Output: