Salesforce | Tips on how to Replace the Values of a Customized Picklist Subject utilizing Metadata API?

The person can simply add, delete, or replace the values of a customized picklist discipline manually. However there are occasions when you must replace and add the values of the customized picklist discipline by utilizing code. That’s when metadata API involves the rescue. For understanding metadata API, you may go to – Metadata, Customized Metadata Varieties, and Metadata API.

Now, for updating the values of the customized picklist discipline there are two courses required:

  1. MetadataService.cls: You’ll find this class out there on the web as effectively however there are modifications finished in accordance with this situation on this class. 
  1. We have to create one other class for updating the values of a customized picklist discipline. The category we’re calling right here is updatingCustomPicklistValues. For this, we’d like a customized Picklist discipline on any object. 

Right here, the article we’ve taken is Alternative and we’ve a customized picklist discipline named Picklist_check 

dont miss out iconRemember to take a look at: Visualizing Salesforce Metadata: Understanding the Relationships with AbstraLinx ERDs

Updating Customized Picklist Values 

public class UpdatingCustomPicklistValues { 
// Methodology to replace the values of customized picklist discipline 
     public static void updatePicklistValues() { 
          MetadataService.MetadataPort service = new MetadataService.MetadataPort(); 
          service.SessionHeader = new MetadataService.SessionHeader_element(); 
          service.SessionHeader.sessionId = UserInfo.getSessionId(); 
          MetadataService.ValueSet picklistValueSet = new MetadataService.ValueSet(); 
          MetadataService.ValueSetValuesDefinition valueDefinition = new MetadataService.ValueSetValuesDefinition(); 
          Checklist<MetadataService.ExtendedCustomValue> values = new Checklist<MetadataService.ExtendedCustomValue>(); 
          // Fetching the values already out there within the customized picklist discipline and storing them within the record 
          Checklist<Schema.PicklistEntry> picklistValues = Alternative.Picklist_check__c.getDescribe().getPicklistValues();
          // Including the already out there values 
               for(Schema.PicklistEntry picklistValue : picklistValues) { 
                    MetadataService.ExtendedCustomValue customValue1 = new MetadataService.ExtendedCustomValue(); 
                    customValue1.fullname = (string)picklistValue.getValue(); 
                    customValue1.default_x = false; 
                    customValue1.isActive = true; 
                    customValue1.label = picklistValue.getLabel(); 
          //Including the additional worth, we wish to add 
          MetadataService.ExtendedCustomValue customValue2 = new MetadataService.ExtendedCustomValue(); 
          customValue2.fullname = ‘1’; 
          customValue2.default_x = false; 
          customValue2.isActive = true;
          customValue2.label = ‘1’; 
          valueDefinition.worth = values; 
          valueDefinition.sorted = true; 
          picklistValueSet.valueSetDefinition = valueDefinition; 
          MetadataService.CustomField customField = new MetadataService.CustomField(); 
          customField.fullName=" Alternative.Picklist_check__c "; 
          customField.label=" Picklist_check "; 
          customField.type_x = 'Picklist'; 
          customField.required = false; 
          customField.distinctive = false; 
          customField.valueSet = picklistValueSet;
          MetadataService.CustomField[] customFields = new Checklist<MetadataService.CustomField> { customField }; 
     // Methodology to avoid wasting the work finished 
     public static void handleSaveResults(MetadataService.SaveResult saveResult) 
          if(saveResult==null || saveResult.success) 
          // Assemble error message and throw an exception 
                    Checklist<String> messages = new Checklist<String>(); 
                    (saveResult.errors.dimension()==1 ? 'Error ' : 'Errors ') + 
                    'occured processing element ' + saveResult.fullName + '.'); 
                    for(MetadataService.Error error : saveResult.errors) 
                    error.message + ' (' + error.statusCode + ').' + 
                    ( error.fields!=null && error.fields.dimension()>0 ? 
                    ' Fields ' + part of(error.fields, ',') + '.' : '' ) ); 
                    throw new MetadataServiceExamplesException( part of(messages, ' ')); 
          if (!saveResult.success) 
          throw new MetadataServiceExamplesException('Request failed with no specified error.'); 
     //Methodology to create an occasion of MetadataService.MetadataPort 
     public static MetadataService.MetadataPort createService() 
          MetadataService.MetadataPort service = new MetadataService.MetadataPort(); 
          service.SessionHeader = new MetadataService.SessionHeader_element(); 
          service.SessionHeader.sessionId = UserInfo.getSessionId(); 
          return service; 
     public class MetadataServiceExamplesException extends Exception { } 

dont miss out iconTry one other wonderful weblog by Navdita right here: What is the Grant Management System in Salesforce? | All You Need to Know

Now , within the Developer console, save each courses. Then, click on on Debug. Then, click on Open Execute Nameless Window. Copy and paste the next code within the nameless window- 


And Yaa, it’s finished. 

Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!