Sub topic 2
Log in to an Org
Assume you can use a terminal and understand Salesforce orgs, metadata, and Apex. You also need the Salesforce CLI installed and access to an org that you can authorize.
Log in to an org
An org is a Salesforce environment. The CLI stores an authorization token locally after you sign in. It then uses that token for commands such as deployments and queries.
Run:
sf org login web --alias myorg --set-default
sf org listThe first command opens a browser. Sign in to Salesforce and approve access. The --alias option gives the org a readable local name. The --set-default option makes myorg the default target org for commands in the current configuration.
sf org list shows the orgs known to the CLI. Check the username, connection status, and alias before running commands that change metadata or data.
Aliases and the default target org
Many sf commands need a target org. The CLI uses the default target when you do not provide one explicitly.
You can change the default and inspect it:
sf config set target-org myorg
sf org displayA common error is sending a deployment to the wrong org because an old default is still configured. Use sf org display before important work. For safer scripts, specify the target directly with the command's target-org flag when supported.
Create a Salesforce DX project
A Salesforce DX project is a local project that organizes source metadata. Its sfdx-project.json file identifies the project and its package directories.
sf project generate --name myproject
cd myprojectThe generated force-app directory commonly holds deployable source. Keep project files under version control so changes can be reviewed.
Deploy and retrieve metadata
Deploy local source to the default org, then retrieve the org's source:
sf project deploy start --source-dir force-app
sf project retrieve start --source-dir force-appDeployment moves local metadata to Salesforce. Retrieval moves metadata from Salesforce into the local project. Avoid editing the same metadata in both places without a plan, because retrieval can overwrite local changes.
Exercise
What does --set-default change when used with sf org login web?
Show answer
It makes the newly authorized org the CLI's default target for commands that do not specify another org.