Sub topic 2
Creating a Salesforce DX Project
Assume you know basic Visual Studio Code, can run terminal commands, and have a Salesforce development org. A Salesforce DX project is a local folder that stores source code and project settings. The Salesforce CLI, sf, uses this structure to deploy and retrieve metadata.
Create the project
Run these commands in a terminal:
sf project generate --name my-apex-project
cd my-apex-project
code .The command creates a project with a force-app directory. Apex classes normally belong under:
force-app/main/default/classes
The sfdx-project.json file identifies the project, its source directory, the login URL, and the Salesforce API version.
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"name": "my-apex-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}The API version controls which Salesforce metadata and language features the project targets.
Authorize and select an org
Authorize an org through the browser and save it with an alias:
sf org login web --alias my-org --set-default
sf org listThe alias is a readable name for the org. --set-default makes this org the target for commands run from the project. Use sf org list to verify that the org is authorized and selected.
A project can also use a different target later. Check the active org before deploying, especially when working with production and sandboxes.
Write Apex with language support
Install the Salesforce Extension Pack in VS Code. Its Apex language server provides completion, symbol navigation, and diagnostics. The language server analyzes Apex in the context of the project and configured org.
Create GreetingService.cls:
public with sharing class GreetingService {
public static String greet(String name) {
if (String.isBlank(name)) {
return 'Hello, world!';
}
return 'Hello, ' + name + '!';
}
}As you type String. or a class name, completion suggests available members. Use Go to Definition to inspect a symbol and Find All References to locate its uses. Red underlines indicate diagnostics such as syntax errors or invalid references. These checks improve editing speed, but deployment and tests remain the final validation.
Exercise
Which command authorizes an org, assigns it the alias my-org, and makes it the default?
Show answer
sf org login web --alias my-org --set-default