+ Start a Discussion

Add new Picklist value with trigger.



I want to write some trigger on Product2 object so that when any new product record is created it can add new picklist value on Picklist__c (Picklist field) under CustomObject__c object.


Any help would be appreciated.





You want to add a new value to the Picklist? If so, that is not possible as far as I know.

I believe that If at all this is possible it is possible via only Meta Data API but not Apex code.

Moreover, If there is no definite/predictable set of values in your picklist then your Data Modeling is wrong. If that is the case you should have made that as custom object itself.

And If there is predictable Super set of Values but you want to show different sub sets of values in different scenarios you should use RecordTypes.

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Nikhil,


You can do this using MetaDataAPI. In MetadataAPI you can add picklist Value.


In following code we add two and three as a picklist value Picklist__c field on Lead.


Use following code in your APEX class:


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 ;
	    MetadataService.UpdateMetadata ut = new MetadataService.UpdateMetadata();
	    ut.metadata= customField;	
	    MetadataService.AsyncResult[] results = service.updateMetadata(new List<MetadataService.updateMetadata> {ut});
public static MetadataService.MetadataPort createService()
		MetadataService.MetadataPort service = new MetadataService.MetadataPort();
		service.SessionHeader = new MetadataService.SessionHeader_element();
		service.SessionHeader.sessionId = UserInfo.getSessionId();
		return service;		

  If this is your solution then please mark as a solution and it helps other for similar issues.


Great. Happened to learn some thing. Thanks

Hi singh,


Can you please explain me how did you update the picklist values by using above class which you have mentioned

when I was trying to save your class in my instance I got below error.

Error: Compile Error: Invalid type: MetadataService.MetadataPort at line 5 column 13

so can you please explain or list out  steps to add the picklist values using apex class.

how the class should be  ?

how can we invoke your code is by using trigger or VF page


Hi ,


have you able to added new picklist values , did you get any soulution.

If you have any solution can you please post me.

I need to also add new picklist values, is it possible by using metadat or apex.

If it is can you please explainme how to do that.

rajesh k 10rajesh k 10


       Using metadata api how to create Multiselect picklist From visualforce page in salesforce?

Hi Bindhyachal Kumar Singh,

I am getting an error like MetaDataService.MetadataPort invalid.Please provide me solution. Thanks in advance.
Thanks a lot Mr.Bindhyachal Kumar Singh
Mamadou Diallo 14Mamadou Diallo 14
Hi Kari, you need to add the MetaDataService.MetadatPort() to your class.