2

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();
            serviceObj.Code_Label__c=strOppcode;
            lstServiceRecord.add(serviceObj);
        }
        try{
            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 Feb 27 '16 at 10:01
  • no.Just a Picklist field.want to add all list values in that picklist field in apex class itself – uma451 Feb 27 '16 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 Feb 27 '16 at 10:06
  • Cant we add all the values in Picklist field?? – uma451 Feb 27 '16 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 Feb 27 '16 at 10:15
4

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>(); 
lststr.add('apple'); 
lststr.add('car'); 
lststr.add('ball'); 

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;
    two.default_x=false;
    customField.picklist.picklistValues.add(two);
}                                           

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.