[SalesForce] Add a custom field to multiple objects

I am working on an assignment to populate the data in a developer
sandbox with production data. This activity involves me in populating
data for over 50 objects. I am planning to create an external Id field
on all objects to store the Id of the records created (A common
approach to populate data in sandbox). So, I need to add a custom
external id field as Production_Id__c on all objects. Is there a way
to add this custom field to multiple objects programmatically, using
Ant or something else?

EDIT:== I finally ended up manually adding the external Id fields to
each objects. Tried the following.

  1. Installed Bulk Object Field Creator App from Salesforce Appexchange
    and it worked fine for 10 objects. It appears that the free version is
    limited only for 10 objects.

  2. Tried the Ultra Schema Builder App from Salesforce Appexchange and
    it did not work as expected. Had it worked fine, my job would have
    been much easier.

  3. Tried Ant Deploy – Got a message saying tools.jar not found as I
    only had JRE. I do not have admin rights on my work laptop and hence
    had to try with the portable JDK. The ANT issue regarding the
    tools.jar is resolved. However, trying to deploy with ANT threw the
    error message "Userid/Password is invalid or locked out". I am behind
    a proxy and my password has special characters ($). Tried escaping the
    $ sign with \$, \$ and also %24 and none of these work (with or
    without security token). Then logged into Salesforce org, found the
    Session Id with the statement UserInfo.getSessionId() and changed the
    build.properties to use the SessionId instead of username and password
    and then got the error message "Failed to send request to Salesforce".

After trying so many options, I finally ended up adding the external
id fields manually to all objects. It was time consuming, but I spent
more than this time doing the research.

Best Answer

Assuming all the objects are already created, you could create a file that simply contains the one field definition you need, copy it, and deploy it. I'd say this task is probably easiest with Ant, but you could conceivably do it in an IDE as well.

First, set up your package.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>CustomObject</name>
    </types>
    <version>38.0</version>
</Package>

And in your objects folder, create a CustomObject__c.object file with the following contents (adjust to suit your own needs):

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<fields>
        <fullName>Comments__c</fullName>
        <description>Add your comments about this object here</description>
        <inlineHelpText>This field contains help text for this object</inlineHelpText>
        <label>Comments</label>
        <length>32000</length>
        <type>LongTextArea</type>
        <visibleLines>30</visibleLines>
</fields>
</CustomObject>

Next, just copy the file over and over again:

$ cp CustomObject__c.object CustomObject2__c.object
$ cp CustomObject__c.object CustomObject3__c.object
  ... as many more as you need

Finally, do your deployment:

ant deploy

Here's a pre-configured repo that you can use to get started.

Related Topic