1

Is there any way to delete specific picklist value by using Metadata API

I've created below code by using MetadataService Class which you can find from here but it returns picklist values as null.

MetadataService.MetadataPort service = createService();

//tried below two ways to query desired custom fields but it returns null
MetadataService.CustomField customField = 
    (MetadataService.CustomField) service.readMetadata(
        'CustomField', new String[] { 'object_name__c.field_name__c' }
    ).getRecords()[0];
MetadataService.CustomField customField = 
    new MetadataService.CustomField();
customField.fullName = 'Object_name__c.field_name__c';
//above two 

system.debug('customField====> '+customField);
system.debug('customField.picklist====> '+customField.picklist);
//this returns null

for(integer i = 0; i < customField.picklist.picklistValues.size(); i++) {
//this line gives null pointer error cause of null.picklistvalues

    MetadataService.PicklistValue pv = customField.picklist.picklistValues[i];
    if(pv.fullName == picklistValue )
    {
        customField.picklist.picklistValues.remove(i);
        break;
    }
}
  • Why do you want to use the API here for just one value? Seems like more overhead than it's worth. – Adrian Larson Feb 10 '17 at 14:37
  • Because it will not be just one value when accomplished. – umithuckan Feb 10 '17 at 14:41
  • I guess this may depend on the version you are using. Do you have FLS access to that field? – Patlatus Feb 10 '17 at 14:42
  • Yes, Patlatus I have. – umithuckan Feb 10 '17 at 14:47
  • What API version are you using? – sfdcfox Feb 10 '17 at 15:07
1

I believe in older API versions (<38) there is no way to delete or modified any picklist value through the Metadata API.

The reason for it is because of data integrity. If the value you want to delete was used in at least one record, only from the configuration interface (point & click) you will be able to address that scenario.

When you are online (point & click), doing that type of operations, the message is something like:

"Use this page to globally replace an existing value in a picklist with a new value. Choose a replacement value in the box below to apply to any records that contain the deleted value. Choose "None" if you do not want to change the value of existing records. The picklist no longer contains the deleted value but records containing that value still display it."

One workaround might be to remove the value at the level of the RecordType(s). Try to use the API to update your RecordType(s) and remove the value.

That was happening with older API versions (cannot say which version previous to 37). With API 38.0 we have the CustomValue and using it, we can Deactivate the values:

If picklist values are missing from a component definition, they get deactivated when deployed. Deactivation occurs for picklist values of both standard and custom fields.

For instance, for this picklist (you can get the file below if you fetch the CustomField and not the CustomObject):

<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fields>
        <fullName>Picklist1__c</fullName>
        <externalId>false</externalId>
        <label>Picklist1</label>
        <required>false</required>
        <type>Picklist</type>
        <valueSet>
            <valueSetDefinition>
                <sorted>false</sorted>
                <value>
                    <fullName>a</fullName>
                    <default>false</default>
                </value>
                <value>
                    <fullName>b</fullName>
                    <default>false</default>
                </value>
                <value>
                    <fullName>c</fullName>
                    <default>false</default>
                </value>
            </valueSetDefinition>
        </valueSet>
    </fields>
</CustomObject>

If we package and deploy the file without the value "c" it will be deactivated.

Before: Picklist before

After: Picklist after

What I cannot see is a way (API method) to delete the value and replace existing values with other active value or "blank".

Globally replace an existing value in a picklist with a new value. Choose a replacement value below to apply to any records that contain the deleted value. If you choose not to replace the value, the picklist no longer contains the deleted value but records containing that value still display it.

enter image description here

  • this isn't true. You can add/replace/delete picklist entries using metadata API – cropredy Feb 11 '17 at 21:05
  • It is true for older API versions. I edited my answer. – Omar Villasmil Feb 12 '17 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.