4

Declarative setup of objects and fields in Salesforce is cumbersome and time-consuming. Is it possible to create objects and add fields using XML or JSON templates, then pushing those changes to a production org?

If so, how is this done? I've scoured for guides on how to do this, but have mostly only found ones that cover ANT.

3 Answers 3

6

You can create fields and objects using the the CustomObject data type.

Using DX is very similar to using Ant. You create a directory to store everything in, make a package.xml manifest file, create the objects folder, add the CustomObjectName.object files, and finally deploy it using a command like the following:

sfdx force:mdapi:deploy -d src -u prod-alias -l RunLocalTests -w -1

If you're careful, you can easily build a bunch of fields at once, but be aware that deploying fields and objects in this manner won't include Field Level Security or page layout changes, so you might also need to create Profile, Layout, CustomTab, and CustomApplication files as well.

While the UI may seem cumbersome, that's because there's a lot of metadata being created all at once. Doing so manually likely won't save you any time, unless you're proficient at scripting and you're willing to script all of the necessary changes. It's possible to build a tool to generate all this data, but you'd have to consider if it's really worth the effort.

I have written a "Permission Management" spreadsheet in Excel with some VB Macros once, but that was to enable us to handle thousands of fields at once, which was far more efficient than manually reviewing field permissions. If you have that volume of data to work with, creating a VB script to load data into/out of Excel or Access might be worth it.

As long as you remember to include all the relevant files linked above, you can do it all programmatically. I'm not aware of any tools out there, free or paid, that actually does all this for you, but it's not too bad to write out, perhaps a few hours worth of coding. I'd say that it's probably just faster to create all the fields once and copy-paste bits and pieces as you need to.

3

I want to add one concrete example to sfdcfox's excellent discussion, just because I've done this before.

If you're in a situation where you need to create hundreds of fields (or even objects), you can effectively incorporate scripting into the process to cut the amount of manual work you have to do, even if other portions of the process are still manual. This applies to both Salesforce DX and regular metadata API deploys, although my example below is for the Metadata API.

One way to approach this that lowers the complexity of the scripting work you have to do is to manually create a "skeleton" object and custom field. Once you pull the source for those entities down (using a package.xml or SFDX), you can build templates from that XML source, even as simply as using string substitution.

I once did a migration from FileMaker into Salesforce, where the primary object had >400 fields. (Yes, this was a huge design problem). I used this quick and very dirty Python script to read a field list CSV and template out Salesforce objects from it. The core mechanism works like this:

Define a dict of templates by field type:

templates["Number"] = """
    <fields>
        <fullName>%(fullName)s__c</fullName>
        <externalId>false</externalId>
        <label>%(label)s</label>
        <precision>18</precision>
        <required>false</required>
        <scale>0</scale>
        <trackTrending>false</trackTrending>
        <type>Number</type>
        <unique>false</unique>
    </fields>
"""

templates["Text"] = """
    <fields>
        <fullName>%(fullName)s__c</fullName>
        <externalId>false</externalId>
        <label>%(label)s</label>
        <length>%(length)s</length>
        <required>false</required>
        <trackTrending>false</trackTrending>
        <type>Text</type>
        <unique>false</unique>
    </fields>
"""

(Note the substitution locations, which could easily be extended to cover more attributes of the fields).

Then, loop over the list of fields that you want to create, and perform string substitution against the appropriate template for the field type:

# Your code will look different depending on how you store the field list.
for key in generatedSchema:
    print(templates[generatedSchema[key]["salesforceType"]] % generatedSchema[key]) 

That just gives you a ton of XML on standard output that you can copy and paste into your skeleton sObject XML file, and push back to Salesforce using the Metadata API.

Note that I am omitting some logic that's based on where the field list is coming from, like doing type and Text field length inference - that will depend on the application.

A similar approach could be used to generate multiple objects, fields across different objects, or to use SFDX format rather than Metadata API.

This doesn't cover the associated entities like tabs, or permission sets and profiles. It's valuable because it can take out a big chunk of work in the middle of the process, not because it handles everything (cleanly or at all). That said, you could extend the approach to generate tab metadata, and there are other tools you can use to help speed up the appropriate configuration of profiles and permission sets.

1
  • an up here for sharing the thoughts and especially the gist. It is so unfortunate that DX doesn't have a command to create object or field. it would have made life so much easier
    – Xi Xiao
    Commented Nov 16, 2018 at 10:34
2

Shane McLaughlin created a DX plugin that has the commands to create obj and field.

I created a POC repo to demostrate it works as expected.

https://github.com/Xixiao007/DX-Data-Mode-Setup

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .