Skip to main content Dedicaci 5 minuti per partecipare al sondaggio della nostra community. Aperto da oggi fino al giorno 11/4/2025. Fai clic qui per partecipare.
Jane G ha fatto una domanda in #Collaboration
I have a picklist field - Status__c. Its values are 'Open', 'Closed', 'In progress'. I want to update the values using metadata api. I'm using the metadataServices.cls but can't update the picklist value. Having error.

 

For e,g update 'Closed' to Close.

 

Can someone please help. Thank you.

 

 
31 risposte
  1. 15 lug 2019, 13:32
    Hi Jane,

     

    Please try this code I have used valueSet to update the picklist value.

     

    (Modify your API name and old/new value and give it a test from the developer console for one value "Closed".)

    String picklistapiname = 'Lead.Status__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];

    // For each on: customField > Get valueset > Get valueSetDefinition > get values

    for(MetadataService.CustomValue objCustomValue : customField.valueSet.valueSetDefinition.value){

         if(objCustomValue.fullName == 'Closed'){ // API Name

              objCustomValue.fullName = 'Close'; // New API Value

              objCustomValue.label = 'Close'; // New Label value

         }

    }

    // Update 

    List<MetadataService.SaveResult> lstResults = service.updateMetadata( new MetadataService.Metadata[] { customField });

    for (MetadataService.SaveResult objResult : lstResults) {

        if (objResult.success) {

            System.debug('Successfully updated');

        }

        else {                       

            if(objResult.errors.size() > 0){

              System.debug('eror : ' + objResult.errors[0].message);

            }

        }

    }

     

    Let's discuss if you any query!
Caricamento in corso
0/9000