Skip to content

Instantly share code, notes, and snippets.

@justin-lyon
Last active December 11, 2019 15:56
Show Gist options
  • Save justin-lyon/eb4b258f15b7b22b9b8674a4705448e7 to your computer and use it in GitHub Desktop.
Save justin-lyon/eb4b258f15b7b22b9b8674a4705448e7 to your computer and use it in GitHub Desktop.

SFDX Cheatsheet

SFDX Commands are workspace specific. When running commands in the terminal, the commands are running within the terminal's current directory. I recommend setting up a /workspace folder that can hold all of your various projects. Salesforce or otherwise.

The Salesforce CLI Command Reference

Contents

  • Create a new Project
  • Create a new SFDX Project
  • Create a new Sandbox Project
  • Command Cheatsheet
  • SFDX Permission Set
  • Remove Hubs and Sandboxes from Machine

Create a new Project

Create a new project for SFDX and Scratch Orgs

New SFDX Project

This is the standard workflow to work with SFDX. Using the long Name flags for readability.

# Navigate to your workspace in terminal
cd path/to/my/workspace

# Create a new project in /workspace/my-project
sfdx force:project:create --name my-project --template standard

# Navigate to my-project
cd my-project

# Login to your Prod org to register it to the my-project workspace
sfdx force:auth:web:login --setdefaultdevhubusername --setalias project-hub

# Create a scratch org to start working in
sfdx force:org:create --setdefaultusername --setalias my-scratch-org --definitionfile config/project-scratch-def.json

# Push your project to the scratch org
sfdx force:source:push

# Pull changes from the scratch org to your project
sfdx force:source:pull

# Open the org in browser
sfdx force:org:open

New Org Model Project (Sandboxes)

This is the workflow to develop in Sandboxes.

Salesforce Org Development Model with VS Code

# Navigate to your workspace in terminal
cd path/to/my/workspace

# Create a new sandbox project in /workspace/my-project
sfdx force:project:create --name my-project --template standard --manifest

# Navigate to my-project
cd my-project

# Login to your sandbox org to register it to the my-project workspace
sfdx force:auth:web:login --setdefaultdevhubusername --setdefaultusername --setalias project-hub

# Retrieve metadata from the sandbox to your project using the package.xml
sfdx force:source:retrieve --manifest manifest/package.xml

# Deploy metadata from your project to the sandbox using the package.xml
sfdx force:source:deploy --manifest manifest/package.xml

# Open the org in browser
sfdx force:org:open

Command Reference

The Salesforce CLI Command Reference

sfdx force:project:create

Create a new project, either an SFDX or an Org Model (Sandbox) project.

-n | --projectname [project-name]

-t | --template [standard | empty]

-x | --manifest (optional - used for salesforce cli dev against a sandbox)

sfdx force:auth:web:login -da <my-hub>

Authenticate to a Hub or Sandbox. The command opens a login page in the browser.

-d | --setdefaultdevhubusername - Defaults are applied only to your working directory, not the entire machine.

-a | --setalias - give a short name to this connection so you can refer to it easily later.

-r | --instanceurl - Optional, provide the full https:// address to your hub or sandbox. Ex: -r https://<my-domain>.my.salesforce.com

sfdx force:org:create

Create a scratch org

-s | --setdefaultusername - Not to be confused with :web:login's --setdefaultdevhubusername, execute contextual commands against this scratch org by default.

-a | --setalias - Set a short name for the scratch org that you can refer to later.

-f | --definitionfile - Which config file to use to init the org, standard projects have it at config/project-scratch-def.json

sfdx force:org:delete

Delete a scratch org. See below to delete Hub and Sandbox references from your machine.

-p | --noprompt - Skip the y/n confirmation

-u | --targetusername - the Alias of the org you want to delete.

sfdx force:source:push

(SFDX) Push your workspace into an org

-u | --targetusername - The alias of the org you want to push to, defaults to the default username

sfdx force:source:pull

(SFDX) Pull changes from your scratch org to your workspace

-u | --targetusername - The alias of the org you want to pull from, defaults to the default username

sfdx force:data:tree:export

Export data by query as json.

-q | --query - The SOQL query to retrieve data EX: -q "SELECT Name, Id FROM Account"

-p | --plan - Generate a JSON Data Plan for a cross object query

-d | --outputdir - The folder in your project to put your exported data.

sfdx force:data:tree:import

Import JSON data to your org

-p | --plan - The data json that you want to import

-u | --targetusername - The alias of the org you want to import data to.

sfdx force:user:permset:assign

Assign a permission set to your user in an org

-n | --permsetname - the api name of the permission set

-u | --targetusername - The alias of the org you want to assign the permset in.

sfdx force:org:open

-u | --targetusername - The alias of the org you want to launch in browser

sfdx force:source:retrieve

(Org Model) Retrieve metadata from the org. Accepts package.xml or individual metadata items.

-u | --targetusername - The alias of the org you want to retrieve from

-x | --manifest - Retrieve with a package.xml in your project

-m | --metadata - Retrieve specific items by type and name

sfdx force:apex:test:run

Execute tests in an org. Reports Test Coverage. Can run specific test classes, methods, or all.

-n | --classnames - Comma delimitted list of Apex Test Class Names to be run

-r | --resultformat - accepts [human | tap | junit | json]

-l | --testlevel - Specifies the test level, should always be set to RunLocalTests

-c | --codecoverage - reports code coverage results

sfdx force:limits:api:display

This is usually used for the dev hub to check sfdx scratch org limits, but can be used for other limits as well.

-u | --targetusername - The alias for the org you want to run against.

sfdx force:org:list

List out the orgs and scratch orgs that you have registered on the machine. Marks orgs (D) and (U) for the current workspace's default hub and scratch orgs.

SFDX Permission Set

The following permisison set allows even a ReadOnly Profile user in Production to use SDFX.

SObject CRUD

  • Active Scratch Orgs, CRUD
  • Scratch Org Infos, RUD
  • Namespace Registries, R

Developers in Prod as ReadOnly Profiles with The SFDX Permission Set.

Salesforce CLI Authentication is compatible with SSO.

Remove a Hub (or Sandbox) from sfdx

Mac/Linux: cd ~/.sfdx

Windows cd %userprofile%.sfdx

cd to the directory for your OS, then delete the .json file of the username that corresponds to your hub org. EX: [email protected]

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