+ Start a Discussion
amar kumar10amar kumar10 

field creation & deletion through metadata api


My requirement is to delete an existing value from picklist, for that i am using Metadata API but still i am not able to perform deletion.
I have created Metadata Service class and added respective method related to deletion and invoking delete operation through Batch for the time being i have hard coded few values like field name. 
Any help will be appriciated.
//Batch Class code
global class UpdatePicklistBatchJob1 implements Database.Batchable<sObject>, Database.AllowsCallouts {
    global String sessionId;
    global UpdatePicklistBatchJob1(String sId) {
        sessionId = sId;
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Object__c, Picklist_New__c, Picklist_Old__c from Picklist_Config__c WHERE Status__c =\'Ready To Process\'');
    global void execute(Database.BatchableContext bc, List<Picklist_Config__c> configs) {
        MetadataSrv.MetadataPort service = new MetadataSrv.MetadataPort();
        service.SessionHeader = new MetadataSrv.SessionHeader_element();
        service.SessionHeader.sessionId = sessionId; // UserInfo.getSessionId();
        // Read Custom Field
        MetadataSrv.CustomField customField = (MetadataSrv.CustomField)
        service.readMetadata('CustomField', new String[] { 'Demo__c.Alternate_Email__c' }).getRecords()[0];
        List<MetadataSrv.DeleteResult> results = service.deleteMetadata('CustomField', new String[] { 'Demo__c.Alternate_Email__c' });
    global void finish(Database.BatchableContext bc) {
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Have you tried calling UpdatePicklistBatchJob1.execute directly from the Anonymous Execution window?  What error(s) are you getting?  Try this in Anonymous Execution and see if it works (or maybe you can debug from the resulting log file):

UpdatePicklistBatchJob1 job = new UpdatePicklistBatchJob1();
job.UpdatePicklistBatchJob1( UserInfo.getSessionId() );
List<Picklist_Config__c> configs =
[   SELECT  Object__c, Picklist_New__c, Picklist_Old__c
    FROM    Picklist_Config__c
    WHERE   Status__c  = 'Ready To Process'
job.execute( null, configs );
amar kumar10amar kumar10
Hi Glyn,
 I am executing this from Anonymous window only and i am not getting any error or exception.
It just not fulfilling the requirement.