I am just trying to see if its possible to add the value to picklist or not. I am getting Attempt to reference null pointer exception on commented line.

Apex class

MetadataService.MetadataPort service = MetadataExamples.createService();
    MetadataService.CustomField customField =
    (MetadataService.CustomField) service.readMetadata('CustomField',
    new String[] { 'sii__Merchandise__c.sii__Test__c' }).getRecords()[0];

    MetadataService.Picklist picklist = customField.picklist;
    List <MetadataService.PicklistValue> picklistValues = picklist.picklistValues; // Error here
    MetadataService.PicklistValue picklistvaluenuevo = new MetadataService.PicklistValue ();
    picklistvaluenuevo.fullName = 'TEST';
    picklistvaluenuevo.default_x=false ;
    picklist.picklistValues = picklistValues;
    customField.picklist = picklist;

    MetadataService.UpsertResult[] results = service.UpsertMetadata(new MetadataService.Metadata [] {customField});
  • Are you getting nay value here customField.picklist;. Is this a picklist field? Commented Sep 26, 2017 at 10:37
  • That's the problem @tusharsharma. customField.picklist is coming as null.My picklist field is sii__Test__c
    – Samir
    Commented Sep 26, 2017 at 10:54
  • I am having a hard time to get this done for custom object. Anyone out there who can help me out?
    – Samir
    Commented Sep 26, 2017 at 12:16
  • 1
    You can get old picklist value using describe easily and then take help from sample code you can create new picklist field easily. Commented Sep 26, 2017 at 12:19
  • I am getting the same error when i am updating the custom as well as standard field on account. Is that the expected behaviour?
    – Samir
    Commented Sep 26, 2017 at 12:53

2 Answers 2


As per discussion on Github by Andrew Fawcett

I have confirmed with Salesforce. You cannot retrieve just CustomFIeld's on their own, you have to retrieve the parent CustomObject.

So you need to retrieve custom objects as well here.

MetadataService.CustomField is always NULL and all Read Metadata Code returns NULL

Sample Code

  • do i need to pass that custom object too as well while the field is being getting updated?
    – Samir
    Commented Sep 26, 2017 at 11:04
  • @Samir never tried this but I think yes. Commented Sep 26, 2017 at 12:15
  • 1
    Alternatively, you could use the Tooling API to query the Metadata field from CustomObject, parse the JSON, then read the values from there.
    – sfdcfox
    Commented Sep 26, 2017 at 13:39

I was able to achieve this using the Sample code Only change required was label name should be correct.

MetadataService.MetadataPort service = MetadataExamples.createService();
        MetadataService.CustomField customField = new MetadataService.CustomField();
        customField.fullName = 'sii__Molecule__c.sii__Color__c';
        customField.label = 'Color';
        customField.type_x = 'Picklist';
        metadataservice.Picklist pt = new metadataservice.Picklist();
        pt.sorted= false;
        metadataservice.PicklistValue two = new metadataservice.PicklistValue();
        two.fullName= 'one';
        two.default_x=false ;
        metadataservice.PicklistValue three = new metadataservice.PicklistValue();
        three.fullName= 'two';
        three.default_x=false ;
        pt.picklistValues = new list<metadataservice.PicklistValue>{two,three};
        customField.picklist = pt ;
        List<MetadataService.SaveResult> results =
                new MetadataService.Metadata[] { customField });

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .