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
Dhaval PanchalDhaval Panchal 

Removing custom fields using Metadata API (Java)

Hi,

 

I am using metadata Api in java code to remove custom fields. I found below code from some blogs and they are mentions it is working code. But it is not working at my end. It always gives me below error.

 

Object with id:04s900000037qo4AAA is InProgress
Error status code: INVALID_CROSS_REFERENCE_KEY
Error message: In field: members - no CustomField named Custom_Field__c found
Object with id:04s900000037qo4AAA is Error

 

Below is the code:

 


    public void deleteCustomField(String fullname) throws Exception
    {
        CustomField customField = new CustomField();
        customField.setFullName(fullname);
        
        UpdateMetadata updateMetadata = new UpdateMetadata();
        updateMetadata.setMetadata(customField);
        updateMetadata.setCurrentName(fullname);
        
        AsyncResult[] asyncResults  = metadataConnection.delete(new Metadata[] {customField});
 
        long waitTimeMilliSecs = ONE_SECOND;
 
        do
        {
            printAsyncResultStatus(asyncResults);
            waitTimeMilliSecs *= 2;
            Thread.sleep(waitTimeMilliSecs);
            asyncResults = metadataConnection.checkStatus(new String[]{asyncResults[0].getId()});
        } while (!asyncResults[0].isDone());
 
        printAsyncResultStatus(asyncResults);
    }

 


    private void printAsyncResultStatus(AsyncResult[] asyncResults) throws Exception {
        if (asyncResults == null || asyncResults.length == 0 || asyncResults[0] == null) {
            throw new Exception("The object status cannot be retrieved");
        }

        AsyncResult asyncResult = asyncResults[0]; //we are creating only 1 metadata object

        if (asyncResult.getStatusCode() != null) {
            System.out.println("Error status code: " +
                    asyncResult.getStatusCode());
            System.out.println("Error message: " + asyncResult.getMessage());
        }

        System.out.println("Object with id:" + asyncResult.getId() + " is " +
            asyncResult.getState());
    }

 

Please help me, I want to mass delete custom fields.

 

Thanks,

Dhaval Panchal

 

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Using ANT and SFDC Migration tool I am now able to delete bulk custom fields.

Download Apache Ant using below link:

Extract and copy this folder where you want to keetp (i have copied this folder to "C" drive : C:\apache-ant-1.9.2)
Set path in environment variables for ant.
To set environment variables
-> Right click on My Computer -> Property -> Advance System Setting -> Click on Environment variables button.
-> click on System variables -> Click on New -> Give variable name = ANT_HOME and variable value = [your and path]
see below images.


Now go to your salesforce account
click on
Setup -> Develop -> Tools -> Force.com Migration Tool.
Download "salesforce_ant_28.0" and extract this zip file.
You will file "ant-salesforce.jar" file in extracted folder, copy this
jar file to "C:\apache-ant-1.9.2\lib"

Now go to folder salesforce_ant_28.0 -> sample
open file "build.properties" and provide your username, password and token there.
In sample folder you will find xml file formates using that you can deploy components.

All Answers

Dhaval PanchalDhaval Panchal

Using ANT and SFDC Migration tool I am now able to delete bulk custom fields.

Download Apache Ant using below link:

Extract and copy this folder where you want to keetp (i have copied this folder to "C" drive : C:\apache-ant-1.9.2)
Set path in environment variables for ant.
To set environment variables
-> Right click on My Computer -> Property -> Advance System Setting -> Click on Environment variables button.
-> click on System variables -> Click on New -> Give variable name = ANT_HOME and variable value = [your and path]
see below images.


Now go to your salesforce account
click on
Setup -> Develop -> Tools -> Force.com Migration Tool.
Download "salesforce_ant_28.0" and extract this zip file.
You will file "ant-salesforce.jar" file in extracted folder, copy this
jar file to "C:\apache-ant-1.9.2\lib"

Now go to folder salesforce_ant_28.0 -> sample
open file "build.properties" and provide your username, password and token there.
In sample folder you will find xml file formates using that you can deploy components.

This was selected as the best answer
o rajuo raju
Hi Dhaval PanchalDhaval,

                                                I created fields using metadata api and i displayed all fields in an visualforce page(Here object name will come through url).Using schema methods i displayed all fields in an visualforce and using wrapper class i added each field as  Delete and edit command links.when i click delete command link how to delete command link delete related field from database.

please help me..................