Dynamic Picklist Values Using Metadata API
1 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.
Interesting code parts for adding and deleting can be found in the addPicklistValue, 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.
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 https://github.com/financialforcedev/apex-mdapi. Click the “Deploy to Salesforce” button and deploy the package into your sandbox.
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 standard approach does not work when fulfilling customers requirements.
My Experiences as an Alumni Teacher of Salesforce Development at ELIS in Rome
5 minute read