I have written a apex class.It will return some List of values.I want add those list of values in the picklist field " Code_Label__c" which is in Service__c Object(custom object).

Public void getOptionCodeLabelPrice() {
       List<string> lstOptionCodeLabel=getPriceListbasedOnReference();
       for(String strOppcode:lstOptionCodeLabel) {
            Service__c serviceObj=new Service__c();
            insert lstServiceRecord; 
        }catch(Exception e) {
            system.debug('Exception :'+e);

here lstOptionCodeLabel contains list of values .I want to add this values in Code_Label__c picklist.But here every values is inserted as a single record.

So basically I need to add list of string in my custom object picklist options.

  • is it a multiselect picklist?
    – Ratan Paul
    Commented Feb 27, 2016 at 10:01
  • no.Just a Picklist field.want to add all list values in that picklist field in apex class itself
    – uma451
    Commented Feb 27, 2016 at 10:02
  • if you want to assign all the values in a picklist field and then you want to insert record ? If yes then you need to assign the value to text field not in picklist field
    – Ratan Paul
    Commented Feb 27, 2016 at 10:06
  • Cant we add all the values in Picklist field??
    – uma451
    Commented Feb 27, 2016 at 10:11
  • If you want to add the multiple values in picklist field then what is the use.. Picklist field meant to be have a multiple option but any one of them can be selected.. I suggest go with multiselect picklist or text field..
    – Ratan Paul
    Commented Feb 27, 2016 at 10:15

1 Answer 1


After discussion with @Uma(I have a request pls update your question with your complete scenario).. So basically what she needs.

There is an API callout that is returning few values she need those values to be in Opportunity Picklist options.

So when user try to create Opportunity record they can see all the options in that picklist.

We can achieve this using Metadata API for updating picklist properties.

Below is the code for updating picklist options with MetaData API

list<string> lststr=new List<string>(); 

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

MetadataService.CustomField customField =
            (MetadataService.CustomField) service.readMetadata('CustomField',
                new String[] { 'Opportunity_Car_Set__c.test__c' }).getRecords()[0];

for(string str:lststr) 
    metadataservice.PicklistValue two = new metadataservice.PicklistValue();
    two.fullName= str;

// Update Custom Field
List<MetadataService.SaveResult> saveResult = service.updateMetadata( 
new MetadataService.Metadata[] { customField });

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .