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.
-
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. -
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. -
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:
And in your objects folder, create a CustomObject__c.object file with the following contents (adjust to suit your own needs):
Next, just copy the file over and over again:
Finally, do your deployment:
Here's a pre-configured repo that you can use to get started.