function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandeep Rahul PVSandeep Rahul PV 

How to Create, Update, Delete Picklist Value Sets through Apex?

Hi Guys,

How do Create, Update and Delete Picklist Value Sets through Apex?

Thanks
Sandeep.

User-added image
PriyaPriya (Salesforce Developers) 
Hi Sandeep,

Greetings!

You would need to use MetadataService class to add the picklist value to your picklist as suggested in the below thread:

https://salesforce.stackexchange.com/questions/65774/how-can-i-add-a-new-value-to-a-picklist-using-apex

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Priya Ranjan
Sandeep Rahul PVSandeep Rahul PV

Hi Priya,

I am getting this error.
User-added image

User-added image
User-added image

My Code is
=========
 

public class updateGlobalPickList {
    public static void updateGlobalPicklist (){
        MetadataService.MetadataPort service =  MetadataServiceExamples.createService();           
        
        MetadataService.CustomField customField = new MetadataService.CustomField();
        customField.fullName = 'Campaign.Advertiser__c';
        customField.label = 'picklist';
        customField.type_x = 'Picklist';
        metadataservice.Picklist pt = new metadataservice.Picklist();
        pt.sorted= false;
        metadataservice.PicklistValue two = new metadataservice.PicklistValue();
        two.fullName= 'second';
        two.default_x=false ;
        metadataservice.PicklistValue three = new metadataservice.PicklistValue();
        three.fullName= 'third';
        three.default_x=false ;
        pt.picklistValues = new list<metadataservice.PicklistValue>{two,three};
            //customField.picklist = pt ;
        List<MetadataService.SaveResult> results =
            service.updateMetadata(
                new MetadataService.Metadata[] { customField });
    }
    
}
Sandeep Rahul PVSandeep Rahul PV

I have changed the code now it is working.
https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls

How to Update Global Picklist through Metadata API
========================================
Class:
=====
public class updateGlobalPickList {
    public static void updateGlobalPicklist (){
        List<String> listSting = new List<String>{'1','2','3','4','5'};
        MetadataService.MetadataPort service =  MetadataServiceExamples.createService();
        MetadataService.GlobalValueSet GlobalValueSet = new MetadataService.GlobalValueSet();
        MetadataService.ValueSetValuesDefinition valueSetValuesDefinition = new MetadataService.ValueSetValuesDefinition();
        metadataservice.valueSet pt = new metadataservice.valueSet();
        pt.valueSetName = 'CampaignAdvertiser';
        List<MetadataService.CustomValue> mcList = new List<MetadataService.CustomValue>();
        for(string s : listSting){
        MetadataService.CustomValue mc = new MetadataService.CustomValue();
        mc.fullName = s;
        mc.default_x = false;
        mc.isActive = true;
        mc.label = s;
        mcList.add(mc);
        }
        GlobalValueSet.fullName = 'CampaignAdvertiser';
        GlobalValueSet.masterLabel = 'CampaignAdvertiser';
        GlobalValueSet.sorted = false;
        GlobalValueSet.customValue = mcList;
        valueSetValuesDefinition.value = mcList;
        valueSetValuesDefinition.sorted = false;
        pt.valueSetDefinition = valueSetValuesDefinition;
        List<MetadataService.SaveResult> results =
            service.updateMetadata(
                new MetadataService.Metadata[] { GlobalValueSet });
    }
}
Test Class
=========
@istest
public class updateGlobalPickListTest {
    private class WebServiceMockImpl implements WebServiceMock 
    {
        public void doInvoke(
            Object stub, Object request, Map<String, Object> response,
            String endpoint, String soapAction, String requestName,
            String responseNS, String responseName, String responseType) 
        {   
            if(request instanceof  MetadataService.updateMetadata_element)
                response.put('response_x', new MetadataService.updateMetadataResponse_element());
            return;
        }
    } 
    
    @IsTest
    private static void coverGeneratedCodeCRUDOperations()
    {	
        // Null Web Service mock implementation
        System.Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
        // Only required to workaround a current code coverage bug in the platform
        MetadataService metaDataService = new MetadataService();
        // Invoke operations     
        Test.startTest();    
        MetadataService.MetadataPort metaDataPort = new MetadataService.MetadataPort();
        Test.stopTest();
    }
    @IsTest
    private static void coverGeneratedCodeFileBasedOperations1()
    {    	
        // Null Web Service mock implementation
        System.Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
        // Only required to workaround a current code coverage bug in the platform
        MetadataService metaDataService = new MetadataService();
        // Invoke operations    
        Test.startTest();     
        MetadataService.MetadataPort metaDataPort = new MetadataService.MetadataPort();
        metaDataPort.updateMetadata(null);
        Test.stopTest();
    }
    @IsTest
    private static void coverGeneratedCodeTypes()
    {             
        Test.startTest();
        System.Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
        new MetadataService();
        new MetadataService.createMetadataResponse_element();
        new MetadataService.DebuggingInfo_element();
        new MetadataService.LogInfo();
        new MetadataService.DebuggingHeader_element();
        new MetadataService.CallOptions_element();
        new MetadataService.AllOrNoneHeader_element();
        new MetadataService.ExtendedErrorDetails();
        new MetadataService.DescribeMetadataObject();
        new MetadataService.DescribeMetadataResult();
        new MetadataService.Error();
        new MetadataService.SaveResult();
        new MetadataService.GlobalPicklistValue();
        updateGlobalPickList.updateGlobalPicklist();
        //Database.executeBatch(new LeadListViewCreationBatch());
        Test.stopTest();
    }
    /* public static testMethod void testschedule() {
Test.StartTest();
LeadListViewCreationBatchSchedule testsche = new LeadListViewCreationBatchSchedule();
String sch = '0 0 10 * * ?';
system.schedule('Test status Check', sch, testsche );
Test.stopTest();
}*/
}