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(); 
                    values.add(customValue1); 
               } 
          //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’; 
          values.add(customValue2);
          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 }; 
          service.upsertMetadata(customFields); 
     } 
     // Methodology to avoid wasting the work finished 
     public static void handleSaveResults(MetadataService.SaveResult saveResult) 
          { 
          if(saveResult==null || saveResult.success) 
          return; 
          // Assemble error message and throw an exception 
          if(saveResult.errors!=null) 
               {
                    Checklist<String> messages = new Checklist<String>(); 
                    messages.add( 
                    (saveResult.errors.dimension()==1 ? 'Error ' : 'Errors ') + 
                    'occured processing element ' + saveResult.fullName + '.'); 
                    for(MetadataService.Error error : saveResult.errors) 
                    messages.add( 
                    error.message + ' (' + error.statusCode + ').' + 
                    ( error.fields!=null && error.fields.dimension()>0 ? 
                    ' Fields ' + String.be part of(error.fields, ',') + '.' : '' ) ); 
                    if(messages.dimension()>0) 
                    throw new MetadataServiceExamplesException(String.be 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- 

UpdatingCustomPicklistValues.UpdatePicklistValues();

And Yaa, it’s finished. 



Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!