2

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.

7

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.

  • Two more things, 1) You also need to include <externalId>true</externalId> if you wanna add it as an external ID 2) By default, no profile can view the field and you have either set the security settings manually or through the ANT by creating the similar setup like above for profiles – Shailesh Patil Dec 23 '16 at 16:12
  • @BalajiPooruli We (answerers) don't get notified of edits, just comments. You probably could have used the workbench, which lets you create a package and upload it as a ZIP file. I could make a demo for you if you'd like? – sfdcfox Dec 28 '16 at 15:17
  • Hi @sfdcfox, could you please share a demo of doing this with the workbench? – Balaji Pooruli Dec 29 '16 at 21:24
  • @BalajiPooruli I created a repo that demonstrates how it's done. The link is in the answer. – sfdcfox Dec 29 '16 at 23:34
  • Hi @sfdcfox, I tried the deployment with workbench and it failed with the error mesage " Must specify a non-empty label for the customObject". – Balaji Pooruli Dec 30 '16 at 21:05
0

Recently found an heroku app https://fieldcreator.herokuapp.com that can be used to create custom fields easily on Salesforce objects. Only caveat is that once the fields are added in salesforce, we need to go to the fields and set the visibility to "true" for the profiles that we want this field to be shown for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.