0

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;    
    }
4
  • What is the error you are having? You have to share your code and approach. Mar 20, 2023 at 9:34
  • Hi, @MukulMahawariya, I have updated the question Mar 20, 2023 at 10:33
  • Are you trying to update a standard field as it is not allowed. Mar 20, 2023 at 12:36
  • @MukulMahawariya Yes I am trying to add a value to picklist field in a SObject Mar 20, 2023 at 15:39

1 Answer 1

0

You are calling the createMetadata method, not updateMetadata and also not handling the result.

I suggest you follow the approach shared in this answer -

MetadataService API custom field label update

If you handle the result properly you will have very specific reason why you are not able to update or create the picklist.

1
  • I tried the above changes, and you were right. I am getting the following error message: "message=Cannot specify label on standard field" Mar 20, 2023 at 21:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .