The way to do this is using the Apex MetaData API wrapper as provided by esteemed SFSE colleague Andrew Fawcett here --
1- Running user needs to have Customize Application privilege so you really want this to be an admin type user 2- Read the examples in the GIT package carefully - there is one for picklists. You need to read the CustomField from the MetaData API first then update, otherwise you'll smash all the existing picklist entries.
public static void updatePicklistField()
{
MetadataService.MetadataPort service = createService();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Lead.picklist__c';
customField.label = 'picklist';
customField.type_x = 'Picklist';
metadataservice.Picklist pt = new metadataservice.Picklist();
pt.sorted= false;
metadataservice.PicklistValue two = new metadataservice.PicklistValue();
two.fullName= 'second';
two.default_x=false ;
metadataservice.PicklistValue three = new metadataservice.PicklistValue();
three.fullName= 'third';
three.default_x=false ;
pt.picklistValues = new list<metadataservice.PicklistValue>{two,three};
customField.picklist = pt ;
List<MetadataService.SaveResult> results =
service.updateMetadata(
new MetadataService.Metadata[] { customField });
handleSaveResults(results[0]);
}
Please select it as Best answer if you find it helpful.
The way to do this is using the Apex MetaData API wrapper as provided by esteemed SFSE colleague Andrew Fawcett here --
1- Running user needs to have Customize Application privilege so you really want this to be an admin type user
2- Read the examples in the GIT package carefully - there is one for picklists. You need to read the CustomField from the MetaData API first then update, otherwise you'll smash all the existing picklist entries.
Please select it as Best answer if you find it helpful.
Thanks
Akshay