Dynamic Picklist Values Using Metadata API

3 minute read

Very often we run into the requirement of needing dynamic picklist values in Salesforce. A common solution involves getting picklist values through the getDescribe method. The values are then used in VisualForce pages or custom controllers. However, having customized VisualForce page instead of a standard page layout, is not always an option.

A different way to tackle this need is to manage picklist values through Metadata API. Metadata API can also be used for managing fields, objects, pages etc., but our interest here lies in the picklists. Suggested usage would be to create/delete picklist values in a trigger or batch job. For the sake of simplicity, the sample code presented here manages picklist values in VisualForce pages and attached controllers.

Interesting code parts for adding and deleting can be found in the addPicklistValue, and deletePicklistValue methods. MetadataService.MetadataPort object is created first. The existing picklist is read as a custom field through the readMetadata method. Picklist value is added/deleted, and the whole custom field is then updated with the updateMetadata method.

Installing Apex wrapper for Metadata API

When installing the Apex wrapper for Metadata API, there are a few things to take into consideration. In order to access Metadata API in Apex code, the Apex wrapper has to be installed into the sandbox. You can find the Apex wrapper, for example, in Click the “Deploy to Salesforce” button and deploy the package into your sandbox.


After installation, you should enable the remote site access in the setup menu: Setup -> Security Controls -> Remote site settings and add the sandbox url.
Salesforce is a rich, powerful and evolving platform. Using Metadata API can further expand the capabilities of Salesforce as a platform. Especially existing solutions can be extended in new ways with Metadata API in places where the standard approach does not work when fulfilling customers’ requirements.

Written by Adam Skorvaga, Senior Software Engineer at Fluido

Read next


My Experiences as an Alumni Teacher of Salesforce Development at ELIS in Rome

5 minute read

Contact us

Contact us