I'm trying to update the picklist value of a custom field in sobj 'Contacts' via the usage of metadata API. So far I was able to:

  1. Generate the apex class form WSDL
  2. fix the issue with session id
  3. Create trigger (user update) to call the class for updating the picklist via metadata API

so when I update the user I could see the session id generated (via debug) and the trigger is called but had an error as below:

FATAL_ERROR|System.CalloutException: IO Exception: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://XXXX.salesforce.com/services/Soap/m/33.0

To fix this i followed the error info and maintained the remote-site setting as:

enter image description here

Following this i see another error after the trigger call:

Web service callout failed: WebService returned a SOAP Fault: Must specify a {http://www.w3.org/2001/XMLSchema-instance}type attribute value for the {http://soap.sforce.com/2006/04/metadata}metadata element faultcode=soapenv:Client faultactor=

My class is:

    public static void UpdatePicklistField(String sessionId)
    MetadataAPIService.MetadataPort service = createService();
    service.SessionHeader.sessionId = sessionId;
    MetadataAPIService.CustomField customField = new MetadataAPIService.CustomField();
    customField.fullName = 'Contact.Test__c';
    customField.label = 'Sales Owner';
    customField.type_x = 'Picklist';
    metadataAPIservice.Picklist sown = new metadataAPIservice.Picklist();
    sown.sorted= false;
    metadataAPIservice.PicklistValue pval = new metadataAPIservice.PicklistValue();
    pval.fullName= 'SHP Picked';
    pval.default_x=false ;
    sown.picklistValues = new List<MetadataAPIService.PicklistValue>{pval};
    customField.picklist = sown ;
    List<MetadataAPIService.SaveResult> results = 
            new MetadataAPIService.Metadata[] { 

Any inputs on what needs to be done here to fix the errors and see the metadata api working to update the picklist values?

1 Answer 1


There's already an API available for you to do it an apex. https://github.com/financialforcedev/apex-mdapi


You must log in to answer this question.

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