Sub topic 1
The Salesforce DX Project and sfdx-project.json
Prerequisites
This chapter assumes basic command-line use, familiarity with Salesforce orgs and metadata, Salesforce CLI (sf) installed, and basic Git knowledge.
Salesforce DX project structure
A Salesforce DX project is a local directory that stores Salesforce metadata in source format. The force-app/main/default directory commonly contains Apex classes, custom objects, fields, and Lightning components.
The sfdx-project.json file identifies the project and its package directories. A minimal example is:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "billing-project",
"namespace": "",
"sourceApiVersion": "60.0"
}The packageDirectories setting tells Salesforce CLI where to find source. The default setting marks the directory used by commands when no other directory is specified. The API version controls the Salesforce metadata format used by the project.
Metadata types and source format
Metadata describes Salesforce configuration and code. Examples include Apex classes, custom objects, fields, permission sets, and Lightning web components.
Source format separates related files into directories. For example, Invoice__c.object-meta.xml describes a custom object, while its fields can be stored below the same object directory. Salesforce CLI converts this source format to the format expected by Metadata API operations.
Authorizing an org
Authorization connects the CLI to a Salesforce org. An alias is a short local name for that org. Set a default target so later commands do not need --target-org each time.
sf org login web --alias my-sandbox
sf config set target-org my-sandboxKeeping files out with `.forceignore`
A .forceignore file excludes files from Salesforce CLI source operations. Use it for local notes, generated files, or metadata that should not be retrieved or deployed.
.DS_Store
*.log
.envDo not ignore files that the project must deploy. An ignored file can make a retrieve or deploy appear incomplete.
Retrieving metadata
A retrieve copies metadata from an org into the local project. Retrieve an entire source directory with:
sf project retrieve start --source-dir force-app/main/default --target-org my-sandboxYou can retrieve specific metadata by type and name:
sf project retrieve start --metadata ApexClass:AccountService --metadata CustomObject:Invoice__c --target-org my-sandboxThe first command selects a local destination. The second selects named metadata components. Check the resulting files before committing them.
Exercise
Which command retrieves the Apex class AccountService into the project?
Show answer
sf project retrieve start --metadata ApexClass:AccountService --target-org my-sandbox