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
PS81PS81 

MetadataService.cls - how to access and use ?

Hi

How to access MetadataService.cls and use it? I want to modify the picklist values and reading couple of post I could understand that using MetadataService.cls i can achieve it but i dont know where to get this MetadataService.cls and how to access it ....an tips please?
Best Answer chosen by PS81
PS81PS81
Session Id needs to be passed as an argument as it is null when called async

All Answers

Ezdhan Hussain S KEzdhan Hussain S K
HI Prabhu,

To change picklist values you need to consume metadata API. Refer below link for more details. 

https://github.com/financialforcedev/apex-mdapi (https://github.com/financialforcedev/apex-mdapi" target="_blank)

If that helps mark this as best answer. cheers..!
PS81PS81
Thanks Ezdhan. But i didnt get my anwsers for the question

1. How to install the Metadata API
2. How to use the metadataAPI

In the examples given, the class is referred to as "MetadataService" but where to get this class from? and how to use for our need?
 
Ezdhan Hussain S KEzdhan Hussain S K
HI Go to setup--> API -->Metadata API. Click on the link and save the XML as WSDL. Go to apex classes--> click --> generate from WSDL. Now you will be having class with methods. Just like the example “metadataservice”.
PS81PS81
Thanks Ezdhan. I was already able to do it but faced issues in creating the class which is not fixed. 

Now, i'm running into error:

"Initial expression is of incorrect type, expected: MetadataAPIService.Metadata"

My code is:
 
public class UpdatePickListSalesOwner {
 public static MetadataAPIService.MetadataPort createService()
    {
        MetadataAPIService.MetadataPort service = new MetadataAPIService.MetadataPort();
        service.SessionHeader = new MetadataAPIService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;    
    }    
    public static void createPicklistField()
    {
        MetadataAPIService.MetadataPort service = createService();
        MetadataAPIService.CustomField customField = new MetadataAPIService.CustomField();
        customField.fullName = 'Contact.Sales_Owner__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 = 
            service.updateMetadata(
                new MetadataAPIService.Metadata[] { customField });
        handleSaveResults(results[0]);
    }
}
any inputs on how to fix this error please?
 
PS81PS81
Now after some code updates i'm gretting an error: 

Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID

any inputs on this please?
PS81PS81
Session Id needs to be passed as an argument as it is null when called async
This was selected as the best answer
Shanmu P 6Shanmu P 6
Am facing the same issue. Please update any solution