Skip to content

Instantly share code, notes, and snippets.

@msrivastav13
Last active June 29, 2024 13:48
Show Gist options
  • Save msrivastav13/6a8273e420a6b375905ea3c0214da488 to your computer and use it in GitHub Desktop.
Save msrivastav13/6a8273e420a6b375905ea3c0214da488 to your computer and use it in GitHub Desktop.

Getting Started with Salesforce CLI

Overview

This tutorial will guide you through the process of installing Salesforce CLI, authenticating to a Salesforce Org, creating a Salesforce project, connecting your development org to the project, retrieving metadata, and deploying metadata from your local machine to the Salesforce org.

What Developers Will Do

  1. Install Salesforce CLI (Skip if using Code Builder)
  2. Authenticate to Salesforce Org via CLI
  3. Create a Salesforce Project
  4. Connect your Development org to the Salesforce Project
  5. Retrieve metadata from the Development Org
  6. Deploy metadata from the local machine to the Salesforce org

Installation of Salesforce CLI

Install Salesforce CLI (via Node.js)

Make sure your computer has Node.js and NPM installed:

node --version
npm --version

Install Salesforce CLI Stable Release:

npm install @salesforce/cli --global

Authenticate to Salesforce Org

Sign up for a Free Developer Edition Org

Sign up for a Free Developer Edition Org

Authenticate to your Salesforce Org

sf org login web -s -a <alias>

Authenticate for Sandbox/Custom Domain

sf org login web -r <instance url> -a <alias>

Create a Salesforce Project & Connect with your Salesforce Org for Development

Generate a Salesforce Project Scaffold

sf project generate -n myProject

Set your Org as the Default Org for Your Project

sf config set target-org=your-org-username@sf.com

Set your Org as the Default Org for Your Project Using Alias

sf config set target-org=<alias>

Retrieving Metadata from the Development Org

Task - Add Metadata to Your Development Org

  1. Build a Custom Object named House
  2. Add two fields: Name and Address
  3. Create a Tab and an App named Dreamhouse

Developer Org/Sandboxes with Source Tracking Disabled

Retrieve Custom Objects

sf project retrieve start -m CustomObject

Retrieve a Specific Apex Class

sf project retrieve start --metadata ApexClass:MyApexClass

Scratch Orgs/Sandboxes with Source Tracking Enabled

Retrieve Metadata Without Specifying Metadata Type

sf project retrieve start

Command to Retrieve Everything from Org

sf project generate manifest --from-org <username/alias>
sf project retrieve start --manifest --from-org package.xml

Deploy Metadata from Local Machine to the Development Org

Task - Create a Simple Apex Class

Generate an Apex Class File

sf apex generate class -n myClass -d force-app/main/default/classes

Deploying Metadata

Developer Org/Sandboxes with Source Tracking Disabled

Deploy a Specific Apex Class
sf project deploy start --metadata ApexClass:MyApexClass
Deploy via Directory
sf project deploy start -p force-app/main/default/classes

Scratch Orgs/Sandboxes with Source Tracking Enabled

Deploy Metadata Without Specifying Metadata Type

sf project deploy start

Explore Other Commands

Help Command

sf --help

Search Command

sf search
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment