All articles

Front-end8 min read

Salesforce from scratch: free org, CLI and VS Code to study

Step by step to set up a free Salesforce environment: Developer Edition, Salesforce CLI, VS Code and your first LWC and Aura components deployed to your org.

Also in Português, Español, Français

You can't learn LWC and Aura just by reading. You need an org to deploy the component to, see it on screen and break things freely. The good news is you can set all of that up for free, in an afternoon.

This is a follow-along tutorial. By the end you'll have your own org, a VS Code project connected to it and two components live: one in LWC and one in Aura. It's the base for part 2, where we put an iframe inside Salesforce and exchange messages with it through postMessage.

An honest note: the commands were checked against Salesforce CLI 2.159, and the rest follows the official documentation, linked at the end. Sign-up screens change over time, so if a button has a different name, go by what it does.

TL;DR#

  • Create a Developer Edition at developer.salesforce.com/signup. It's free and it's yours.
  • Install the Salesforce CLI (sf), VS Code with the Salesforce Extension Pack and a JDK 21 for Apex.
  • Create the project with sf template generate project and connect it to the org with sf org login web.
  • Generate, deploy and put it on screen: sf template generate lightning component, sf project deploy start and the Lightning App Builder.
  • Old tutorials use sfdx force:.... Those commands were retired. Today everything starts with sf.

What you'll have at the end#

  • a Developer Edition org with your user;
  • the Salesforce CLI installed and authenticated to it;
  • a Salesforce DX project open in VS Code;
  • an LWC (helloWorld) and an Aura component (helloAura) deployed and visible on a page.

1. Create your org#

An org is your "Salesforce": an environment with users, data, settings and the code you deploy. To study, the right choice is the Developer Edition, which is free.

  1. Open developer.salesforce.com/signup.
  2. Fill in name, email and company (it can be "Studies").
  3. For Username, use an email-like format that's only yours, such as yourname@salesforce-study.dev. It doesn't need to be a real mailbox, but it must be unique across all of Salesforce, so maria@gmail.com is probably taken.
  4. Confirm the email that lands in your inbox and set the password.

Keep that username. It's what you use to log in to the org and to connect the CLI.

If you do Trailhead modules, you'll also see the Trailhead Playground. It's meant for the challenges there. For this tutorial, the Developer Edition is simpler, because you log in directly with username and password.

2. Install the tools#

Salesforce CLI#

It's the command line that creates the project, connects to the org and deploys code. Two ways, pick only one (installing both ways confuses the PATH, and the documentation itself warns about it):

  • Installer: download the .pkg (macOS) or .exe (Windows) from the CLI download page and follow the wizard.
  • npm: if you already have Node.js LTS installed:
npm install @salesforce/cli --global

Close and reopen the terminal and check:

sf --version

VS Code and the extensions#

  1. Install VS Code.
  2. In the extensions tab, search for Salesforce Extension Pack (published by Salesforce) and install it.
  3. Install a JDK 21. The Apex extension needs Java to work. If VS Code doesn't find it on its own, point to the Java folder in the salesforcedx-vscode-apex.java.home setting (the installation folder, not the executable).

3. Create the project#

In a terminal, in the folder where you keep your projects:

sf template generate project --name salesforce-study
cd salesforce-study
code .

The CLI creates a Salesforce DX project. What matters for now:

salesforce-study
salesforce-study/
├── force-app/main/default/   # everything that goes to the org: lwc/, aura/, classes/, staticresources/...
├── sfdx-project.json         # project config (API version, default folder)
├── .forceignore              # what never goes to the org
└── package.json              # Jest, ESLint and Prettier already set up

If you follow an older tutorial, you'll see sf project generate or sfdx force:project:create. The first one still works, but the CLI warns that it was replaced by sf template generate project. The second one is from the old generation of the CLI, which stopped getting updates in 2023.

4. Connect the project to the org#

sf org login web --alias study --set-default

The browser opens on the Salesforce login screen. Log in with the username and password from step 1 and click Allow. You can close the tab afterwards.

  • --alias study gives the org a short nickname, so you don't type the username every time.
  • --set-default makes it the project's default org: the next commands go to it without you having to say so.

To check:

sf org list

The study org shows up in the list. And to open the org in the browser, already logged in:

sf org open

5. Your first LWC#

Generate the component inside the lwc folder:

sf template generate lightning component --name helloWorld --type lwc --output-dir force-app/main/default/lwc

The CLI creates helloWorld.html, helloWorld.js, helloWorld.js-meta.xml and a test in __tests__. Replace the content of the first three:

helloWorld.html
<template>
    <lightning-card title="Hello, Salesforce">
        <div class="slds-p-horizontal_medium">
            <lightning-input label="Your name" value={name} onchange={handleChange}></lightning-input>
            <p class="slds-m-top_small">Hi, {name}! This LWC came from your VS Code.</p>
        </div>
    </lightning-card>
</template>
helloWorld.js
import { LightningElement } from 'lwc';
 
export default class HelloWorld extends LightningElement {
    name = 'dev';
 
    handleChange(event) {
        this.name = event.target.value;
    }
}
helloWorld.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>68.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

The js-meta.xml file is what confuses people most at the start. The CLI generates it with isExposed set to false, and that way the component exists in the org but doesn't show up anywhere. isExposed set to true plus the list of targets say which pages it can be placed on.

Deploy:

sf project deploy start --source-dir force-app

The CLI shows a table with what went up. If there's an error, it points to the file and the line.

Put the component on screen#

  1. Run sf org open.
  2. On the Home page, click the gear in the top corner and then Edit Page. The Lightning App Builder opens.
  3. In the list on the left, under Custom, drag helloWorld onto the page.
  4. Click Save. The first time it asks about activation: choose Activate and set it as the org default.
  5. Go back to Home. The card is there, and the text changes as you type.

6. Your first Aura component#

Now the same path in Aura, so you can see both side by side:

sf template generate lightning component --name helloAura --type aura --output-dir force-app/main/default/aura

Aura generates more files (controller, helper, renderer, design, CSS, documentation, icon). To start, only the .cmp and the controller matter:

helloAura.cmp
<aura:component implements="flexipage:availableForAllPageTypes" access="global">
    <aura:attribute name="name" type="String" default="dev" />
 
    <lightning:card title="Hello, Aura">
        <div class="slds-p-horizontal_medium">
            <lightning:input label="Your name" value="{!v.name}" />
            <p class="slds-m-top_small">Hi, {!v.name}! This component is Aura.</p>
        </div>
    </lightning:card>
</aura:component>

Notice two differences:

  • Aura has no js-meta.xml. What makes the component available in the App Builder is the flexipage:availableForAllPageTypes interface.
  • value="{!v.name}" already binds the field both ways. In LWC you write the onchange and update the property by hand. It's more code, but it's clear where each change comes from.

Deploy again and put helloAura on the same page, the same way as before:

sf project deploy start --source-dir force-app

7. The day-to-day loop#

From here on, the rhythm is always the same:

  1. edit the file in VS Code;
  2. deploy only what changed, to go faster;
  3. reload the org page.
Deploy takes the code to the org. Retrieve brings back what you changed in the org
sf project deploy start --source-dir force-app/main/default/lwc/helloWorld

In VS Code you can do step 2 without a terminal: right-click the file or folder and choose SFDX: Deploy This Source to Org.

And when you change something directly in the org (a new field, a page in the App Builder) and want to bring it into the project:

sf project retrieve start --metadata FlexiPage

Common problems#

  • The component doesn't show up in the App Builder. In LWC, check isExposed set to true and the target for the right page. In Aura, check the flexipage:availableForAllPageTypes interface.
  • I deployed, but the screen didn't change. Lightning keeps a cache in the browser. For studying, it's worth turning it off: in Setup, search for Session Settings and uncheck the secure and persistent browser caching option. In your study org, this only makes pages a bit slower.
  • "This username already exists". The username is unique across all of Salesforce. Make up a more specific email-like format.
  • The Apex extension complains about Java. Install JDK 21 and point to the folder in salesforcedx-vscode-apex.java.home.
  • A tutorial tells you to run sfdx force:.... That's the old CLI. Look for the sf equivalent, such as sf project deploy start instead of sfdx force:source:deploy.

Next step#

With the org up and both components live, part 2 puts an external page inside Salesforce with an iframe and gets both sides talking through postMessage: in LWC, in Aura and with lightning:container, which is Aura's official way to do it. It includes the Trusted URLs setting that allows the iframe and the origin check that keeps the conversation safe.

Checklist#

  • Developer Edition created, email confirmed, password set.
  • sf --version responds.
  • VS Code with Salesforce Extension Pack and JDK 21.
  • Project created with sf template generate project.
  • sf org login web --alias study --set-default done, org showing in sf org list.
  • helloWorld with isExposed set to true and targets.
  • helloAura with flexipage:availableForAllPageTypes.
  • Both deployed with sf project deploy start and visible on Home.

References#

Comments

Sign in with your GitHub account to comment.