All, I am trying to add a value to a picklist field of an entity (i.e. Document Attachment Status of Care Request Extension ) via apex class in my org only. I tried following approaches but those are not working: How to add new values to a picklist from apex code How can I add a new value to a picklist using APEX? Is there a proper documentation or any API for the above purpose?
Below is the code snippet of the approach which is I am trying right now, It is getting compiled successfully but not updating the picklist value of CaseSubStatus field of CareRequestExtension:
public void UpdatePicklistField(){
MetadataService.MetadataPort service = createService();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'CareRequestExtension.CaseSubStatus'; // your object. fields name
customField.label = 'CaseSubStatus'; // label of field.
customField.type_x = 'Picklist'; // type
metadataservice.ValueSet two = new metadataservice.ValueSet(); //new value
two.valueSetName = 'second';
customField.valueSet = two;
List<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { customField });
}
public MetadataService.MetadataPort createService(){
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
return service;
}