1
votes

I have a picklist field in some object. And, through apex, I want to set the selected value of the picklist. When I try to do that, it says "bad value for restricted picklist field:". but the value I am trying to set is there in the value set. And I have enabled "Restrict picklist to the values defined in the value set" too.

     MyObject.MyPickListField = 'Belgium';

May I know where I have gone wrong in setting the picklist field's selected value ?

1
Can you share your value set ? - Noor A Shuvo
Hi ! It's like this. 'Belgium','Canada', 'Sri Lanka'. I have edited my question accordingly. - Prasadika
These are labels. Do you think I need to assign the value instead ? - Prasadika

1 Answers

0
votes

To resolve this issue, remove the default value from the Record Type or picklist field (depending on where it is set).

Remove the default value: Record Type

Lightning Experience instructions:

  • Select the Gear icon | Setup
  • Click Object Manager
  • Select the Object for the Record Type that contains the picklist
  • Select Record Types
  • Select Record Type that contains the picklist
  • Click Edit for the picklist you wish to change
  • In the 'Default' field, select --None--
  • Click Save

Salesforce Classic instructions:

  • Select Setup | Customize
  • Select the Object containing the Picklist, then select Record Types
  • Click the name of the Record Type that contains the picklist
  • Click Edit for the picklist you wish to change
  • In the 'Default' field, select --None--
  • Click Save

Remove the default value: Picklist

Lightning Experience instructions:

  • Select the Gear icon | Setup
  • Click Object Manager
  • Select the Object that contains the picklist
  • Select Fields and Relationships
  • Select the picklist field you wish to change
  • Click Edit for the picklist value that is set as the Default
  • Deselect the 'Default' checkbox. Note: Make this value the default for the master picklist.
  • Click Save

Salesforce Classic instructions:

  • Select Setup | Customize
  • Select the Object containing the picklist, then select Fields
  • Select the picklist field you wish to change
  • Click Edit for the picklist value that is set as the Default
  • Deselect the 'Default' checkbox. Note: Make this value the default for the master picklist.
  • Click Save