Sub topic 6
Creating a Salesforce DX project
This chapter assumes you have already installed VS Code, the Salesforce CLI (sf), a Java JDK, and the Salesforce Extension Pack from the earlier chapters. You should be comfortable running commands in a terminal and know what Apex is. By the end, you will have a working Salesforce DX project open in VS Code, and you will know what each file and folder in it does.
What a Salesforce DX project is
A Salesforce DX project is a plain folder on your computer that holds your Salesforce metadata as source files. Metadata is the configuration and code that defines an org: Apex classes, triggers, Lightning components, custom objects, and so on. The folder is normally kept in version control such as Git. The org is then a deployment target, and the project folder is the source of truth.
The Salesforce extensions only activate their full feature set when VS Code has a DX project open. The extensions recognize a project by one file at its root: sfdx-project.json. If that file is missing, commands such as deploy and retrieve are unavailable or fail, and the Apex language server has no source folders to index.
Creating a project from the command line
Open a terminal in the folder where you keep your code, not inside another project. Then run:
sf project generate --name my-apex-project
cd my-apex-project
code .The first command creates a new folder named my-apex-project and fills it with the standard layout. The second moves into it. The third opens it in VS Code. If code is not found, open the Command Palette in VS Code and run Shell Command: Install 'code' command in PATH. This is mainly a macOS issue.
By default, sf project generate uses the standard template. You can pass --template to choose another one. The empty template creates fewer files, and analytics is intended for Tableau CRM work. For Apex development, the standard template is the right choice.
Creating a project from VS Code
The editor has an equivalent command. Open the Command Palette (Ctrl+Shift+P on Windows and Linux, Cmd+Shift+P on macOS) and run SFDX: Create Project. VS Code asks you for three things in order:
- The project template. Choose Standard.
- The project name.
- The parent folder in which to create it.
VS Code then opens the new project in the current window. Both approaches run the same CLI command underneath, so the result is identical. The CLI route is easier to script and to reproduce in documentation. The command route saves you from switching windows.
If you already have a DX project, for example one cloned from Git, skip this step. Use File > Open Folder and select the folder that contains sfdx-project.json.
The generated layout
After generation, the project looks roughly like this:
| Path | Purpose |
|---|---|
| `sfdx-project.json` | Project configuration. Marks the folder as a DX project. |
| `force-app/` | The default package directory. Your metadata source lives here. |
| `force-app/main/default/classes/` | Apex classes and their `-meta.xml` files. |
| `force-app/main/default/triggers/` | Apex triggers. |
| `config/project-scratch-def.json` | Definition file for scratch orgs. |
| `scripts/` | A place for helper scripts. It includes sample Apex and SOQL files. |
| `.vscode/` | Workspace settings and recommended extensions. |
| `.forceignore` | Patterns for files that deploy and retrieve should skip. |
| `.gitignore` | Patterns for files Git should skip. |
| `package.json` | Optional Node tooling such as Prettier and ESLint. |
Two folders appear later and should not be committed. The CLI creates .sfdx/ (and in newer versions .sf/) to hold local state such as cached data and logs. Both are listed in .gitignore by default.
Folder names and metadata files
Source in force-app/main/default is organized by metadata type. Each Apex class is a pair of files:
GreetingService.clsholds the code.GreetingService.cls-meta.xmlholds the API version and status.
You never need to write the XML file by hand. The SFDX: Create Apex Class command creates both. Do not delete or rename one file without the other, because the deploy will fail or produce an unexpected result.
Understanding sfdx-project.json
This file is the one piece of configuration the tooling reads before anything else. 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 key has a specific job:
| Key | What it does |
|---|---|
| `packageDirectories` | A list of folders that contain source. Deploy and retrieve commands work on these folders. Exactly one entry must have `"default": true`. |
| `name` | A label for the project. It does not have to match the folder name. |
| `namespace` | The namespace of a managed package. Leave it empty unless you build a namespaced package. |
| `sfdcLoginUrl` | The login URL used when you authorize an org. |
| `sourceApiVersion` | The API version used when source is deployed and when new metadata files are created. |
packageDirectories in practice
Most beginners use one directory, force-app. Larger projects split code into several directories, for example core and sales, so that each can become its own package. You add an entry to the array and create the folder:
{
"packageDirectories": [
{ "path": "core", "default": true },
{ "path": "sales", "default": false }
],
"name": "my-apex-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}New components created from VS Code go into the default directory. Deploy commands that use --source-dir accept any folder, whether or not it is listed. Retrieve commands that use --manifest or --metadata write to a package directory, so you should list every folder that holds source.
sfdcLoginUrl and My Domain
The default login URL is correct for production and Developer Edition orgs. For a sandbox, you normally pick the sandbox login when you authorize, as covered in the next chapter. If you always work in sandboxes, you can set sfdcLoginUrl to https://test.salesforce.com. Some teams instead set it to their My Domain URL, for example https://mycompany.my.salesforce.com. Either way, a wrong value only affects the login step. It does not affect deploy or retrieve once the org is authorized.
sourceApiVersion
This value decides which API version your metadata is deployed with. Keep it at or below the version your org supports. A sandbox that has not yet been upgraded to a new Salesforce release rejects a project that targets the newer version. The error usually says the version is not valid for the org. The fix is to lower sourceApiVersion or wait for the sandbox to be upgraded.
The sf CLI can also show the effective value. Run the command below from the project root:
sf config listThis lists configuration values such as target-org that you set in later chapters. It confirms that the CLI sees the project and its local settings.
Excluding files with .forceignore
.forceignore works like .gitignore, but it controls what deploy and retrieve process. Each line is a pattern. The generated file already skips items such as package.xml and some Lightning bundle test folders. You add your own patterns when you want to keep something in version control but out of the org, for example local notes:
# Local notes that should never be deployed
**/notes/**A pattern that is too broad is a common mistake. For example, *.cls would silently stop all Apex from deploying. If a deploy reports "No results found" or no changes when you expect some, check .forceignore first.
Verifying that the project is recognized
Run these checks before moving on:
- In VS Code, open the Explorer and confirm
force-app/main/defaultis visible. - Open the Command Palette and type
SFDX:. You should see a long list of commands, including SFDX: Create Apex Class and SFDX: Authorize an Org. - Look at the status bar at the bottom. You should see the org selector, which shows "No Default Org Set" until you authorize one.
- Create the sample class from this tutorial. Run SFDX: Create Apex Class, name it
GreetingService, and accept the default folder. Then replace the generated body with:
public with sharing class GreetingService {
public static String greet(String name) {
if (String.isBlank(name)) {
return 'Hello, world!';
}
return 'Hello, ' + name + '!';
}
}Save the file. The matching GreetingService.cls-meta.xml file should already exist next to it. You deploy this class in a later chapter.
If the SFDX: commands are missing, wait a few seconds, because the extensions activate lazily, then reload the window with Developer: Reload Window. If they are still missing, confirm that the open folder contains sfdx-project.json at its top level.
Common problems when creating a project
- "A project with that name already exists." The target folder is not empty. Choose a new name or a different parent folder.
- Creating a project inside another project. Nested projects confuse both Git and the extensions. Always generate from a neutral parent folder.
- Spaces or unusual characters in the path. Most tools cope, but some scripts and older CLI plugins do not. Prefer short paths without spaces, especially on Windows.
- Wrong
sourceApiVersionafter cloning. A project generated years ago may carry an old version. It still deploys, but new metadata features may be unavailable. Update the value deliberately and test. - Committing local state. Make sure
.sfdx/and.sf/stay in.gitignore.
Exercise
You cloned a repository and opened its parent folder in VS Code. The SFDX: commands for deploy and retrieve do not appear. What is the likely cause, and how do you fix it?
Show answer
The extensions look for sfdx-project.json at the root of the opened folder. Because you opened the parent folder, the file is one level down and the project is not detected. Use File > Open Folder and select the folder that directly contains sfdx-project.json, then reload the window if the commands still do not appear.
Exercise
A teammate adds a second entry to packageDirectories with "default": true and leaves the original entry also marked "default": true. What is wrong, and what rule does it break?
Show answer
Exactly one package directory must be marked as the default. Two defaults make the project file invalid, so CLI commands that read it will fail with an error. Set "default": true on only one entry and false (or remove the key) on the others.