Skip to content

Instantly share code, notes, and snippets.

@sf-wilson
Forked from jsmithdev/sfdx-cheatsheet.sh
Last active March 14, 2024 09:51
Show Gist options
  • Save sf-wilson/64c5d7b8332647dfcd1b1bbdaed652cf to your computer and use it in GitHub Desktop.
Save sf-wilson/64c5d7b8332647dfcd1b1bbdaed652cf to your computer and use it in GitHub Desktop.
Salesforce SFDX Cheat Sheet
# This cheatsheet contains the most often used SFDX commands for beginners to get a jumpstart.
# Hint. it is highly recommended to use `-h` to check the usage of any SFDX commands and corresponding parameters.
# For instance, use `sfdx force:auth:web:login -h` to checke what `-d` `-a` parameters do
# List down all supported dx commands:
sfdx force:doc:commands:list
# List org web / jwt auth method
sfdx auth:list
# Check current DebHub and Scratch Org status
sfdx force:org:list
# Log into DevHub via the web browser
sfdx force:auth:web:login -d -a DevHub
# Log out all connected orgs
sfdx force:auth:logout -a --noprompt
# Set alias for a connected environment. Format: [alias=username]
sfdx force:alias:set DevHub=xi.xiao@gmail.com
# change password for a org with alias "tempTest"
sfdx force:user:password:generate -u tempTest
# Log into a sandbox environment
sfdx force:auth:web:login --setalias sandbox1 --instanceurl https://test.salesforce.com
# Set one Dev Hub as the default
# Note `defaultdevhubusername` can be either username or DevHub alias
sfdx force:config:set defaultdevhubusername=DevHub
# Create a scratch org with alias tempTest
sfdx force:org:create -s -f config/project-scratch-def.json -a tempTest
# Set an existing scratch org as default
sfdx force:config:set defaultusername=<username|alias>
# run the LTS tests constructed in the app named jasmineTests.app
sfdx force:lightning:test:run -a jasmineTests.app
# Open the scratch org with alias 'tempTest' in browser
sfdx force:org:open -u tempTest
# Open the default scratch org with lightning experience
sfdx force:org:open --path one/one.app
# Delete a scratch org with alias tempTest
# Note. `-u` accepts either an username or a scratch org alias
sfdx force:org:delete -u tempTest
# Create a project locally in folder MyProject
sfdx force:project:create -n MyProject
# Check changes between the scratch org and local project
sfdx force:source:status
# Pull changes from the associated scratch org
sfdx force:source:pull
# Retrieve an Apex class from the org by mdapi
sfdx force:source:retrieve --metadata ApexClass:MyNewClass
# Push changes from the associated scratch org
sfdx force:source:push
# Assign a permission set to the current logged in user
sfdx force:user:permset:assign -n myCustomPermSet
# Export some sample data from the scratch org
sfdx force:data:tree:export -q "SELECT Name, Location__Latitude__s, Location__Longitude__s FROM Account WHERE Location__Latitude__s != NULL AND Location__Longitude__s != NULL" -d ./data
# Import sample data to the current associated/default scratch org
sfdx force:data:tree:import --sobjecttreefiles data/Account.json
# Scaffold a lightning App structure
sfdx force:lightning:app:create -n AccountLocatorApp -d force-app/main/default/aura/
# Scaffold a lightning component structure
sfdx force:lightning:component:create -n myComponent -d force-app/main/default/aura
# Scaffold a lightning event structure
sfdx force:lightning:event:create -n myEvent -d force-app/main/default/aura
# Install LTS testing framework
# More about LTS: https://forcedotcom.github.io/LightningTestingService/
sfdx force:lightning:test:install
# Update sfdx version
sfdx update
# Convert source code to a package. Be ready to deploy to sandbox
sfdx force:source:convert -d mdapi/ --packagename package_name
# Deploy to connected environment without running test; wait 5mins for the report
sfdx force:mdapi:deploy -d mdapi/ -u xisb3 -l NoTestRun -w 5
# Check connected environment deploying job result
sfdx force:mdapi:deploy:report
# To deploy all Apex classes:
sfdx force:source:deploy -m ApexClass
# To deploy a specific Apex class
sfdx force:source:deploy -m ApexClass:MyApexClass
# To deploy all Apex classes and two specific profiles (one of which has a space in its name)
sfdx force:source:deploy -m "ApexClass, Profile:My Profile, Profile: AnotherProfile"
# To deploy all components listed in a manifest
sfdx force:source:deploy -x path/to/package.xml
# Deploy specified code from sandbox to production
sfdx force:source:deploy -m ApexClass:OnePage_ListingController -l RunSpecifiedTests -r ApexClassTest1,ApexClassTest2 -w 3 -u pro
# Deploy via package.xml from sandbox to production
sfdx force:source:deploy -x manifest/package.xml -l RunSpecifiedTests -r ApexClassTest1,ApexClassTest2 -w 33 -u pro --verbose --loglevel fatal
# Run all unit tests with code coverage statistics
sfdx force:apex:test:run -cy -l RunAllTestsInOrg -w 5 -r human
# assume a package.xml containing list of meta-data is in place, the goal is to retrieve meta-data from sandbox and conver to a DX format so we can save these metadata to git as well as push into scratch org
sfdx force:mdapi:retrieve -r retrieved -k manifest/package.xml -w 10 -u xxx@xxx.com
# Unzip the retrieved metadata zip file into a folder named "retrieved".
unzip retrieved/unpackaged.zip -d retrieved
# Delete the zip file
rm retrieved/unpackaged.zip
# then move the unpacked folder to a DX project root path
sfdx force:mdapi:convert -r retrieved/unpackaged -d force-app
# sfdx force:mdapi:convert --rootdir unpackaged/
# above command will convert metadata info and save to current DX project
# to find the version of the CLI engine
sfdx --version
# to find the version of the CLI plugin
sfdx plugins --core
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment