Using the metadata api apex wrapper (https://github.com/financialforcedev/apex-mdapi)
you can modify values of picklist.
Steps are as following:
1- Deploy the metadata api apex wrapper into your org.
2- Modify the CustomValue class included in the MetadataService class including this new values:
public virtual class CustomValue extends Metadata {
public String type = 'CustomValue';
public String fullName;
private String[] fullName_type_info = new String[]{'fullName',SOAP_M_URI,null,'0','1','false'};
public String color;
public Boolean default_x;
public String description;
public Boolean isActive;
public String label;
private String[] color_type_info = new String[]{'color',SOAP_M_URI,null,'0','1','false'};
private String[] default_x_type_info = new String[]{'default',SOAP_M_URI,null,'1','1','false'};
private String[] description_type_info = new String[]{'description',SOAP_M_URI,null,'0','1','false'};
private String[] isActive_type_info = new String[]{'isActive',SOAP_M_URI,null,'0','1','false'};
private String[] label_type_info = new String[]{'label',SOAP_M_URI,null,'0','1','false'};
private String[] apex_schema_type_info = new String[]{SOAP_M_URI,'true','false'};
private String[] field_order_type_info = new String[]{'fullName','color','default_x','description','isActive','label'};
}
3-Create your code to modify your picklist values as desired. I attach a code to for example modify the value 'WebCampaign2019'
of
Source_Specification__c
picklist of the object Account to be 'WEBCAMPAIGN2019'.
String picklistapiname = 'Account.Source_Specification__c';
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
MetadataService.CustomField customField = (MetadataService.CustomField) service.readMetadata('CustomField', new String[] { picklistapiname}).getRecords()[0];
System.debug(customField.valueSet);
for(MetadataService.CustomValue picklistValue : customField.valueSet.ValueSetDefinition.value){
if(picklistValue.fullName!=null && picklistValue.fullName.equals('WebCampaign2019')){
picklistValue.fullName = 'WEBCAMPAIGN2019';
picklistValue.Label = 'WEBCAMPAIGN2019';
}
}
List<MetadataService.SaveResult> lstResults = service.updateMetadata( new MetadataService.Metadata[] { customField });
4- Done.
