Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Integration and Deployment

Deploying Apex and LWC with the Salesforce CLI

Learn source-driven Salesforce development: SFDX project layout, scratch orgs, sf project deploy, running Apex tests, package.xml and CI/CD, with a Java deploy-order stand-in.

Writing Apex in the browser's Developer Console is fine for a quick experiment, but real projects keep code in version control and move it between orgs through a repeatable process. Salesforce calls this source-driven development, and its tool is the Salesforce CLI (sf), together with VS Code and the Salesforce Extension Pack. This lesson explains the project layout, the org types, the commands you use every day, and how a pipeline ties it together.

The CLI and orgs are not available on this site. The shell commands below are read-only reference (we do not claim exact output). The runnable examples are Java stand-ins for two ideas: deploying components in dependency order, and the test-coverage gate.

Project layout

sf project generate --name my-project creates a Salesforce DX project:

Text
my-project/
  sfdx-project.json          project settings and package directories
  force-app/main/default/
    classes/                 Apex classes (.cls and .cls-meta.xml)
    triggers/                Apex triggers
    lwc/                     Lightning web components
    objects/                 custom objects and fields
    permissionsets/
  config/project-scratch-def.json   scratch org shape
  .forceignore               files the CLI must skip

Everything (code, objects, fields, layouts, permission sets) is stored as text metadata files, so it can be diffed, reviewed and merged in Git like any code. Each Apex class has a companion .cls-meta.xml with its API version and status.

Kinds of org

Org Use
Scratch org A short-lived, disposable org created from a definition file; ideal for feature work and CI
Developer sandbox A copy of production metadata for team development
Partial or Full sandbox Realistic data for UAT and performance testing
Production The live org; deployments run tests

Daily commands

Text
sf org login web --alias mydevhub --set-default-dev-hub
sf org create scratch --definition-file config/project-scratch-def.json --alias feature1 --duration-days 7
sf project deploy start --source-dir force-app --target-org feature1
sf project retrieve start --source-dir force-app --target-org feature1
sf apex run test --target-org feature1 --test-level RunLocalTests --code-coverage --result-format human --wait 10
sf apex run --file scripts/seed.apex --target-org feature1
sf org open --target-org feature1
  • deploy pushes local source to the org; retrieve pulls changes made in the org (for example via point-and-click) back to your files.
  • sf project deploy start --dry-run --test-level RunLocalTests validates against an org without saving, a standard step before production.
  • --manifest manifest/package.xml deploys exactly the components listed in a manifest, handy for hand-picked releases. sf project generate manifest can create one.
  • Use .forceignore to leave out things you do not want deployed.

Test levels and the coverage gate

A deployment to production must run tests:

  • NoTestRun is only allowed in sandboxes and scratch orgs.
  • RunSpecifiedTests runs the named test classes (each class in the deployment needs coverage of at least 75 percent from them).
  • RunLocalTests runs all tests in your org except managed-package tests.
  • RunAllTestsInOrg runs everything.

The org-wide coverage must be at least 75 percent, and all selected tests must pass. A single failing test blocks the deployment. See Apex test classes.

Deployment order and dependencies

Metadata depends on other metadata: a trigger needs its object, a class needs a custom field it references, an LWC calling an Apex controller needs the class. The CLI works out the order inside a single deploy, but if a dependency is missing in the target org the deploy fails with an error naming the component. Keep changes together in one deploy, and remember the pending scheduled-job rule: a class with a scheduled job cannot be replaced until the job is aborted (see Scheduled Apex).

Release flow

flowchart LR
    A["Feature branch"] --> B["Scratch org: deploy + test"]
    B --> C["Pull request + CI validate"]
    C --> D["Sandbox: UAT"]
    D --> E["Production: validated deploy"]

Each step uses the same source and commands; only the target org changes.

A CI server (GitHub Actions, GitLab CI, Azure DevOps) logs in with a JWT-based connected app, creates a scratch org, deploys, runs all tests and reports coverage on every pull request. Other tools in the ecosystem include change sets (point-and-click, between related orgs), unlocked packages (versioned, modular units of metadata) and DevOps Center.

Try the idea: deploy in dependency order (Java stand-in)

Java Stand-in: order components so dependencies deploy first (Java)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The field comes first, then the classes that use it, then the trigger and component. The real CLI does this resolution for you inside one deployment.

Try the idea: the coverage gate (Java stand-in)

Java Stand-in: block a deploy under 75 percent coverage or with a failing test (Java)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The numbers pass the 75 percent bar overall (even with one weak class), but the second run fails because of a single failing test.

Habits that save pain

  • Commit everything to Git; treat orgs as disposable build targets.
  • Deploy small and often.
  • Validate against production with --dry-run before the release window.
  • Keep secrets out of the repo; use environment variables and a secure store in CI.
  • Pin the API version in sfdx-project.json and update it on purpose.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which org type is created from a definition file and is disposable?

    Scratch orgs are short-lived, configurable orgs ideal for feature development and CI.
  2. 2. What is the minimum Apex code coverage to deploy to production?

    At least 75 percent overall coverage is required, and all executed tests must pass.
  3. 3. What does sf project retrieve start do?

    Retrieve brings org metadata down to your project; deploy pushes source up.
  4. 4. What is the purpose of --dry-run on a deploy?

    A dry run checks that the deploy would succeed, including tests, without committing the changes.

Summary

  • Keep metadata as source in Git (SFDX project) and treat orgs as targets.
  • Use scratch orgs for features, sandboxes for UAT, and validate before production.
  • Core commands: deploy start, retrieve start, apex run test, org create scratch.
  • Production deploys need all tests passing and 75 percent coverage; automate with CI.

Next: Project: A Trigger Handler Framework