While upgrading the managed package we often forgot about the picklist field which has a limitation after winter 18 release. This can be quite tricky because we can add the values in picklist field and upgrade the package. The issue is realised when upgrading the package in subscriber Org when values are not upgraded. This is a generalised issue while upgrading any package containing picklist fields.
I faced this issue with one of the project involves update a manage package. While solving the issue I thought why not create a solution that is compatible with any package (managed or unmanaged) and can be independent.
I have used Metadata API services and created a solution to add new picklist values in custom Field. To make it independent I have used custom metadata to get the object field and new picklist values
Steps to follow to use the code
1. Add classes and custom metadata in your manage package.
2. Add records (object name, field name, picklist value) in custom metadata.
3. Run UpdateFieldValues.UpdateFieldValues();
Classes : MetadataService.cls, UpdateFieldValues.cls
Custom Metadata: Picklist_Field_Definition__mdt
In the example below we have a custom field status__c on lead object.
Existing picklist values are 1, 2, closed.
To add a new value in the picklist i.e ‘Open’ we need to add a record in custom metadata Picklist_Field_Definition__mdt check the screenshot below.
After adding the picklist in custom metadata we need to run method UpdateFieldValues.UpdateFieldValues() while upgrading.
After running the method the new value ‘Open’ will be added.
Here is the final result:
If you have any questions please feel free to contact us.