|Table of Contents|
|Agenda: Clone custom objects in Salesforce|
|Testing the Hypothesis and Steps to proceed|
|Clone custom fields in salesforce from Standard Object|
|Clone custom fields in salesforce from Custom Object|
|Clone Standard fields in salesforce|
How to clone custom objects in Salesforce
So you started working on a nice project but it has huge data movement to and from and eats your time creating Fields and Objects!
But life is never a cakewalk, right? Or can it be if you can clone them?
You are sure to come across a situation where you need to make a custom object which is very similar to an existing one in your org. It has a similar set of fields defined and those are bulk in volume. The business requirement could be anything. Like a client who cares least to segregate data over a composite API and wants you to make an alternate object with all fields straightened up at one place so that he can send everything in one spot while you hassle to run batch jobs and displace it at proper spots. Anyway…
You must have realized by now that to fulfill this you will need to manually make a very bulky data-carrying object with fields in numbers of several tens, or even hundreds. But just before you think of installing any third-party app just to copy-paste/clone custom objects in Salesforce for you. I have a very handy tip to share.
Agenda : Clone custom objects in Salesforce, definitely also to clone fields in salesforce Org.
Required tool : Visual Studio Code
So well lemme give you some idea first. When we deploy components from org to org we basically get the data in XML from one spot and then put it in place in another to make the move. We need to put it in the right place in the architecture to make the deployment successful.
In a similar fashion, if we could just fetch the fields that we intend to clone in our custom object in Salesforce and then put it back with alteration needed in the same org, won’t we just meet the need? Let’s take that idea for a test run now.
Testing the Hypothesis
We open VSCode and Authorise the org we intend to work with. To start off make sure you delete the default file as highlighted to avoid moving any past data.
Now we open the package.xml file under manifest and we write the xml to retrieve the data we want to clone. Need not be scared, it all has a format and we just need a little alteration. In the member tag we add the component names and in the name tag we add the type of the component. Be sure to be accurate about this. One way is to check it via workbench to avoid unncessary errors later.
Lets create the XML as required. Trying it out with few standard fields first. Ensure to CTRL+S to save the package XML before retrieving.
As we can see we have retrieved the required and we see the location is the default file. Let’s open that in the system and look what we have.
We see the folder sequence, so under objects folder have Account object folder and under that are our retrieved fields. Lets manipulate the object folder and rename account folder name with our custom object where we want our fields cloned. So rename the folder with exact CustomCloneObject__c as per need.
As I have to clone fields in salesforce from 2 objects of both custom and standard type objects in a selective manner, I have already created a blank object.
Now we have to use convert command to have that default file saved at our system be converted for final deployment into the org.
After hitting ENTER you will get a location for the file and it would also appeatr at the left side of the vscode console as a file named clonetest(any name you give it in the CLI window).
Let’s go ahead and use deploy command and deploy it back to same org.
Checking the deployment status in org, it seems we have an issue.
Investigating it, I came to an understanding that all the fields I tried were standard field and were not able to fit in as custom field. So I tried renaming the field’s XML files as “Package_FieldName__c” and redid the whole process. And…
Following the error and comparing the XML of a standard and a custom field, I get to understand the XML file of a standard field doesnot have all the defintions for the field that can be imparted directly to the custom field. We are retrying this later in this post.
Clone custom fields in salesforce from Standard Object
So first let’s clone custom fields in salesforce org. (Here from Standard object in salesforce org)
Here we see a success! So here is How to clone custom object and fields in salesforce from a standard object. I have followed the same exact process as I had explained above. Retreiving the required in default folder, name change to the desired clone object in salesforce and then convert and deploy back from CLI commands.
HOT Issue – Recreating fields in this manner wont set FLS for the fields, you can either create a further permission set or follow this post to change FLS from XML.
Clone custom fields in salesforce from Custom Object
Also, Let’s go ahead and in the same manner clone fields in salesforce org from a custom object.
For any small error like I got here was ‘There is already a Child Relationship named XYZ on User.‘ You can just open the xml in visual code, under the default>Object>Fieldname and do the name changes. Deploying again and Voila..!
Also 26+11 fields are a total of 37 fields but the system just updates the duplicates if and when found so we have 35 distinct ones.
Clone Standard fields in salesforce
Now coming on How to clone Standard fields in Salesforce Org, that in our first part didn’t work. It is not much useful though to confirm the hypothesis run, I found a workaround. Just retrieve any field of similar type from any object/fields in your VSCode to get a sample XML. I retrieved a text and Phone field and went back to the raw file and made its dupes. Then I renamed them with the desired field names in the XML, to create the standard text fields and phone fields in salesfirce.
For a demonstration, you can see the below XML 2 where i modified type of text(50) to 255 and also renamed the field names. Same way, converted phone type XML to Email just by editing the type in XML 1.
File names automatically change to the required field name when you change the XML. Now deploying this folder will clone standard fields in salesforce.
But using this might not be your prefernce, as its individually renaming and editing the files but if you have a quick hand on Windows, you can do it faster this way rather than on salesforce UI. It works for me, so I did.
Cloning standard fields or custom fields in this fashion can save you a lot ot time. I ended up cloning 44+ fields while writting this post in a details in 1 hour along with my evening tea and snack session.
Lemme know in the comments if such posts are handy or just drop something kind…
6 thoughts on “How to effortlessly clone custom objects in Salesforce using VSCode”
Thanks a lot for this demo.
I followed your steps but the deployment failed, and my Custom Object completely disappeared. I tried to recreate it but received an error message telling me that the object already existed. Any clue what could have gone wrong? Thanks a lot for your support!
So there are always some tweeks because this aint a formal way given by salesforce . It was a small experiment i did. If you could exactly tell me what all happened in sequence i might be of some help
Nevermind, my object got renamed with the name of the one that I cloned, I must have forgotten to edit the label in the files 😉
Yup there are backfires