Sub topic 6
Creating a Salesforce DX project
This chapter assumes you have already installed VS Code, the Salesforce CLI, a Java JDK, and the Salesforce Extension Pack. It also assumes you are comfortable running commands in a terminal. By the end, you will have a project folder that the Apex tooling recognizes, and you will know what sfdx-project.json controls.
What a Salesforce DX project is
A Salesforce DX project is a folder with a fixed layout and a configuration file named sfdx-project.json at its root. The extensions and the sf CLI look for that file to decide that a folder is a Salesforce project. Without it, commands such as deploy and the Apex language server features will not work correctly.
Source code lives in your local folder, and orgs are targets you deploy to or retrieve from. This is called source-driven development. Your files are the source of truth, and version control tracks them.
Create the project
You have two ways to create a project. Both produce the same result.
In VS Code, open the Command Palette and run SFDX: Create Project. Choose the Standard template, enter a project name, and pick a parent folder. VS Code then opens the new project.
From a terminal, run this:
sf project generate --name my-apex-project
cd my-apex-project
code .The last line opens the folder in VS Code. If code is not found, run Shell Command: Install 'code' command in PATH from the Command Palette on macOS, or reinstall VS Code with the PATH option on Windows.
Look at the generated layout
The generator creates several files and folders. These are the ones you will use most.
| Path | Purpose |
|---|---|
| `sfdx-project.json` | Project configuration read by the CLI and extensions |
| `force-app/main/default/` | Default location for your metadata, such as `classes/` and `triggers/` |
| `config/project-scratch-def.json` | Scratch org definition, used only if you create scratch orgs |
| `.forceignore` | Files the CLI should skip when deploying and retrieving |
| `.sf/` and `.sfdx/` | Local state, such as the default org and cached data. Do not commit these. |
Apex classes go in force-app/main/default/classes/. Each class needs two files: the .cls file with your code, and a .cls-meta.xml file with its API version and status. The SFDX: Create Apex Class command creates both for you.
Read sfdx-project.json
Open sfdx-project.json. A typical file looks like this:
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"name": "my-apex-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Each field has a specific job:
packageDirectorieslists the folders that hold your source. Only files under these paths are deployed. Exactly one entry should be marked"default": true.nameis the project name.namespacestays empty unless you work with a namespaced package.sfdcLoginUrlis the login URL used when you authorize an org. Change it tohttps://test.salesforce.comif you mainly work with sandboxes.sourceApiVersionis the API version used when you deploy and retrieve. It also affects the metadata files the tooling generates.
How the pieces connect
The extensions call the CLI for most actions, so both read the same project file.
Add a first class
Create GreetingService.cls with SFDX: Create Apex Class, using force-app/main/default/classes as the folder. Replace its body with this code:
public with sharing class GreetingService {
public static String greet(String name) {
if (String.isBlank(name)) {
return 'Hello, world!';
}
return 'Hello, ' + name + '!';
}
}Later chapters use this class for code completion, deployment, testing, and debugging.
Common mistakes
- Opening the parent folder. Open the folder that directly contains
sfdx-project.json. If you open a folder above it, the extensions may not activate. - Committing local state. The generated
.gitignorealready excludes.sf/and.sfdx/. Keep it that way. - Editing the API version blindly. Setting
sourceApiVersionhigher than your org supports causes deploy errors.
Exercise
You create a class in a folder named scripts/ at the project root, and it never deploys with the rest of your source. What in sfdx-project.json explains this, and how would you fix it?
Show answer
Only paths listed in packageDirectories are deployed, and scripts/ is not listed. Either move the class under force-app/main/default/classes/ or add scripts as another entry in packageDirectories.