LearnFrenzy
Sign in Take a free test

Getting Started with Apex · Lesson 2 of 41

Apex Development Setup: VS Code, Salesforce CLI and Orgs

5 min read Updated Nov 8, 2026 Saurabh Samir

You can write a few lines of Apex in the browser, but real development happens in VS Code with the Salesforce CLI: your code lives in files under version control, you deploy with one command, and tests run from the editor. This lesson sets that up and shows the three ways you'll run Apex day to day.

In this lesson

  • Which kind of org to practise in
  • Installing VS Code, the extensions and the sf CLI
  • Creating a project and connecting an org
  • Running anonymous Apex and reading debug logs

Step 1: get an org

Org typeBest for
Developer Editionfree, permanent, your personal practice org – sign up at developer.salesforce.com
Trailhead Playgroundfree orgs tied to Trailhead badges
Scratch orgtemporary orgs created from a Dev Hub by the CLI – ideal for team development and CI
Sandboxa copy of your company's production org for development and testing

Never write code directly in production

Production orgs don't allow editing Apex directly anyway; code arrives by deployment, and only with 75% overall test coverage. Practise in a Developer Edition org or a sandbox.

Step 2: install the tools

  1. VS Code from code.visualstudio.com.
  2. The Salesforce Extension Pack (Expanded) from the VS Code marketplace – Apex language support, debugger, test runner and more.
  3. The Salesforce CLI (sf) from developer.salesforce.com/tools/salesforcecli.
  4. A Java JDK (17 or 21), which the Apex language server needs.
# check the CLI
sf --version

# create a project
sf project generate --name apex-course
cd apex-course

# log in to your Developer Edition org and make it the default
sf org login web --alias devorg --set-default

Step 3: the project structure

An SFDX project

  • apex-course/
    • sfdx-project.json – project settings, API version
    • force-app/main/default/
      • classes/ – AccountService.cls + AccountService.cls-meta.xml
      • triggers/ – AccountTrigger.trigger + its -meta.xml
      • lwc/, objects/, …
    • scripts/apex/ – anonymous Apex scripts (hello.apex)
    • config/ – scratch org definitions
# create a class (or use Command Palette → "SFDX: Create Apex Class")
sf apex generate class --name HelloApex --output-dir force-app/main/default/classes

# deploy your changes to the org
sf project deploy start --source-dir force-app

# run tests
sf apex run test --class-names HelloApexTest --result-format human --synchronous

Step 4: run anonymous Apex

Put a snippet in scripts/apex/hello.apex and run it – from VS Code (select the code → "SFDX: Execute Anonymous Apex with Editor Contents") or from the terminal:

sf apex run --file scripts/apex/hello.apex
// hello.apex – saved in scripts/apex
System.debug('Running as a ' + UserInfo.getUserType() + ' user');
System.debug('Org edition: ' + [SELECT OrganizationType FROM Organization].OrganizationType);
System.debug('SOQL queries used: ' + Limits.getQueries() + ' of ' + Limits.getLimitQueries());

Debug log

Running as a Standard user
Org edition: Developer Edition
SOQL queries used: 1 of 100

The CLI prints the full debug log. In this course, result panels show just the System.debug lines – the part you care about – plus any error that stopped the code.

Debug logs and log levels

  • A debug log records what happened in a transaction: SOQL queries, DML, method calls, limits used and your System.debug output.
  • To capture logs for code that runs on its own (a trigger fired by a user, a batch job), add a trace flag for that user: Setup → Debug Logs, or sf apex tail log to stream logs live.
  • Log levels (per category: Apex Code, Database, Workflow…) control detail. Higher levels mean bigger logs; logs are capped at 20 MB.
  • System.debug(LoggingLevel.ERROR, 'message') lets you set a level per message.

Developer Console: the quick option

In any org, the gear menu → Developer Console gives you a browser-based editor: Debug → Open Execute Anonymous Window to run snippets, the Query Editor tab for SOQL, and the Logs tab for debug logs. It's handy for a quick check, but for real work use VS Code: your code is in files, under Git, and deployable to any org.

Practice

Try it yourself

  1. Install the tools, log in to a Developer Edition org, and run the hello.apex script above. What does Limits.getQueries() report, and why?

    Show answer

    1 of 100 – the query on Organization used one of the transaction's 100 SOQL queries.

  2. A trigger you deployed isn't behaving, but nothing appears in Setup → Debug Logs. What's missing?

    Show answer

    A trace flag for the user who triggered it. Logs are only captured for users (or the Automated Process user) with an active trace flag.

Quick check

Which command deploys local source to an org?

sf project deploy start.

What's the free, permanent org for practice called?

Developer Edition.

What test coverage does a production deployment need?

At least 75% across the org's Apex, and every trigger must have some coverage.

Summary

  • Practise in a Developer Edition org, a scratch org or a sandbox – never production.
  • Use VS Code, the Salesforce Extension Pack and the sf CLI.
  • Run snippets as anonymous Apex and read the debug log.
  • Add trace flags to capture logs for code that runs automatically.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links