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
vinod fnuvinod fnu 

how to assign value to a multi picklist field using apex.

i am trying to write a test class to cover a multipicklist field.
i am not able to pass the multiple value using apex.
AnudeepAnudeep (Salesforce Developers) 
Hi Vinod,

Here is an example.

In the example below MultiPicklist__c is a multi-select 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 the multi-select picklist field.
public class MutlipicklistFieldValue {
    public MutlipicklistFieldValue () {
    }
    public void insertAccount() {
        Account newAccount = new Account();
        newAccount.name = 'Testing Multipicklist Value';
        string values = 'A;B;D';
        newAccount.MultiPicklist__c = values;
        insert newAccount;
        system.debug('newAccountnewAccount'+newAccount);
    }
}

Let me know if it helps

Thanks, 
Anudeep
vinod fnuvinod fnu
I tried the same.But it is giving the error
first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, 
AnudeepAnudeep (Salesforce Developers) 
Cause of the 'Bad Value' error

This error occurs when the picklist field referenced in the error message meets each of the following conditions:
  1. The referenced picklist field has 'Restrict picklist to the values defined in the value set' set to Enabled.
  2. The referenced picklist field is a dependent picklist.
  3. The page layout does not include both the controlling and dependent picklist fields.
  4. A default value is set for the referenced picklist field:
  5. If the object includes Record Types - This error occurs if the record type sets the default value.
  6. If the object does not include Record Types - This error occurs if the picklist field sets the value.
  7. All record types should have the newly create picklist selected in the Selected items section and not under available items.
  8. Initiate a Mass Quick Action with Predefined Value Set
  9. Any active trigger on the object where the field is used can give the same error.
Let me know if this helps

http://https://help.salesforce.com/articleView?id=000320321&language=en_US&type=1&mode=1


Thanks, 
Anudeep
vinod fnuvinod fnu
@Anudeep
Thanks for the reply
I was missing the party to get the picklist values in the test class.
 DescribeFieldResult describe = MyCustomObject__c.customfield__c.getDescribe();
List<PicklistEntry> avl = describe.getPicklistValues();
  MyCustomObject__c. h1 = new MyCustomObject__c.(name='abc');
string fgh=avl[0].value+';'+avl[2].value+';'+avl[1].value;
h1.customfield__c=fgh;

in this way I was able to get the code coverage.