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
sen19sen19 

Rename API name of existing custom fields

I am trying to rename existing field's API name. I know that before that I have to remove all references from class, VF pages..etc. Currently I am trying to fetch all references like classes and field metadata (xml file) in VS code and trying to deploy back with the updated name but it is creating new fields instead of overwriting the existing field. Can any one let me know that how should I proceed? shall I have to remove all references in the org manually then change the field API name. Post that put back all changes again manually?
Best Answer chosen by sen19
ShirishaShirisha (Salesforce Developers) 
Before,we need to change the API name of the field.we have to complete the below steps to make changes successfully.Otherwise,it will throw the error as the API name has been used in the apex classes and you can't complete the request.

1.Removal of the usage of Field/Object in the metadata, i.e Apex Classes, Triggers, Visualforce Pages, Visualforce Components etc. will be required, and then the components will be needed to be changed in order to refer to the new API Name of the Field/Object.

2.If there are any Webservice Callouts which are being carried out via Apex Code, you will need to first remove the usage, and then change the API names in the code.

As you already know,we can search all the references in VS code.Once,you have all the list of components where you have referenced the field,you need remove the field refences.

Then,you need to login to the salesforce and update the API name in the Org which can be possible in the Production as well.

Once,you have done the update in the UI then you can update the code in VS code and deploy it into the Production.

Hope this is clear now.

Thank you!

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Ananga,

Greetings!

Yes,it will create the new custom field as we are trying to change the API name of the field based on which the field will search for the existing field whenever we try to update the API name using any other tool.

However,you can search for the references using VS code and remove the references of the field and go ahead and update the API name of the field manually in the org and then update it with the New API name using the VS code.

Reference:https://help.salesforce.com/articleView?id=000314907&type=1&mode=1

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
 
sen19sen19
@Shirisha, Thanks! so this should be done manually. Then how can we move this changes to production as we can not change the apex classes in prod directly.
ShirishaShirisha (Salesforce Developers) 
Before,we need to change the API name of the field.we have to complete the below steps to make changes successfully.Otherwise,it will throw the error as the API name has been used in the apex classes and you can't complete the request.

1.Removal of the usage of Field/Object in the metadata, i.e Apex Classes, Triggers, Visualforce Pages, Visualforce Components etc. will be required, and then the components will be needed to be changed in order to refer to the new API Name of the Field/Object.

2.If there are any Webservice Callouts which are being carried out via Apex Code, you will need to first remove the usage, and then change the API names in the code.

As you already know,we can search all the references in VS code.Once,you have all the list of components where you have referenced the field,you need remove the field refences.

Then,you need to login to the salesforce and update the API name in the Org which can be possible in the Production as well.

Once,you have done the update in the UI then you can update the code in VS code and deploy it into the Production.

Hope this is clear now.

Thank you!
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi Ananga,

If you want to do this on Production, than please follow the below steps:
  1. Remove all the references from the components using the vs code.
  2. Add the fields that you want to replace in the postdestructivechanges.xml file.
File name should be destructiveChangesPost. Sample code for this file is as below:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>Object.FieldName</members>
        <name>CustomField</name>
    </types>
	<version>48.0</version>
</Package>
What it will do is, it will delete the old fields after the deployment. Please make sure this xml file is kept at the same place where package.xml is stored.
Let me know if you need any other help.

Thanks,
Abhishek Bansal.