1

With the current state of the DX CLI, is it possible to dynamically add objects that have custom properties?

Example - I create a scratch org then install the base package. From thereon, would I be able to create say an Account object and give it custom fields through the CLI?

I haven't seen anything in the API documentation about such a thing, but I'm new to Salesforce in general so wondering if an approach for this is not supported?

Or can you only do such a thing once every package you need that contains the custom fields you want/need for objects have been installed?

Hoping if there is even a hacky way about the above, I'd love to know. Thinking of leveraging that possibility with javascript and powershell.

Any clarity greatly appreciated.

1 Answer 1

4

The Salesforce DX CLI can do things at a few different levels. It manages scratch and non-scratch orgs, for example, and it handles deploying and retrieving source. On top of those basic capabilities, it has some tools that handle building specific types of metadata, like sfdx force:apex:class:create.

Salesforce DX is a source-based system, and you can always create metadata source code yourself using any tool you please. Convenience commands like the above make it easier, but don't preclude you from simply writing an Apex class and its associated XML metadata to disk and then performing a deployment.

sfdx does not offer commands to programmatically scaffold the metadata for a custom object or add fields to a custom object, as it does for Apex, Visualforce, and Lightning components. However, it's not particularly difficult to generate or template out the XML definitions of that metadata yourself.

The easiest way to figure out what the metadata needs to look like is just to pull some examples from one of your orgs, or you can look through an existing project like those in the trailheadapps collection on GitHub. Bear in mind that Salesforce has two source code formats (Metadata API and SFDX), so you may see some confusing differences between older and newer projects. You'll start out with SFDX format source code if you're building your project anew with SFDX.

Once you have the metadata in hand, SFDX will happily push it into your scratch org, sandbox, or production org using the sfdx force:source:push (scratch orgs/source-tracking orgs), sfdx force:source:deploy (non source-tracking orgs with SFDX source format), and sfdx force:mdapi:deploy (any org with Metadata API source format) commands.

You can mix-and-match package installations and deployments of metadata from SFDX. Just bear in mind that if you install a managed package you're still subject to the usual limitations on altering managed components.

3
  • 4
    The shane-sfdx-plugins plugin for the cli facilitates creating that object and field metadata from the command line. Unofficial and unsupported, but written by a Salesforce employee. Apr 25, 2019 at 22:37
  • That's pretty snazzy. Adding to my sfdx now...
    – David Reed
    Apr 25, 2019 at 22:40
  • That plugin set looks sweet based on the docs. Considering a Salesforce employee wrote it, is that safe to say the cli at current state has zero support out of the box?
    – trebleCode
    May 2, 2019 at 2:00

You must log in to answer this question.

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