The Salesforce CLI (Command Line Interface) is a powerful tool that simplifies development and deployment tasks on the Salesforce platform. Whether you’re new to Salesforce or an experienced developer, understanding some key commands can drastically improve your workflow. In this article, we will explore the top 10 essential Salesforce CLI commands every developer should know.
1. Authenticate to Salesforce Org
sf org login web --set-default --alias <alias> --instance-url <instance_url>
Description: This command logs you into a Salesforce org via the web browser, where you can authenticate to a specific Salesforce instance. After successful login, it sets that org as the default for future CLI operations and assigns it an alias for easy reference. You can also specify the --instance-url
option to log into different Salesforce instances (e.g., production, sandbox, etc.).
Use Case: This command is highly beneficial when you need to log into multiple Salesforce environments (such as production and sandboxes) and want to avoid manually entering credentials every time. By using the --alias
option, you assign an easy-to-remember name for that org, and the --set-default
flag ensures this org becomes your primary target for all subsequent CLI commands.
Example: This command logs you into a production org via the Salesforce login page, assigns it the alias “my-prod-org”, and sets it as the default org for future commands.
sf org login web --set-default --alias my-prod-org --instance-url https://login.salesforce.com
Use Case Scenario: Let’s say you are working on multiple orgs like a sandbox and a production org. You can use this command to log in to your sandbox (https://test.salesforce.com
) and production (https://login.salesforce.com
) instances easily. By setting an alias and making one of them the default, you can efficiently switch between orgs without having to log in every time, ensuring commands target the correct org. This is especially useful during development, testing, and deployment cycles.
2. List all Salesforce Orgs
sf org list
Description: The sf org list
command displays a list of all Salesforce orgs that you have authenticated into using Salesforce CLI. The list includes the org aliases, usernames, org types (e.g., scratch, sandbox, production), and the status of each org (such as whether it’s active or expired). It also highlights the default org, if any, that is currently set for CLI operations.
Use Case: This command is extremely useful when you’re working with multiple Salesforce orgs and need to quickly check which orgs are available, their types, and whether they are active or expired. It provides a clear overview of all your orgs without needing to manually track them. The command is helpful in multi-org environments where switching between orgs frequently is necessary.
Use Case Scenario: Imagine you’re working on a project that involves development in multiple scratch orgs, testing in a sandbox, and deploying to a production org. By running sf org list
, you can easily see all your authenticated orgs, their aliases, and their statuses. If a scratch org has expired or if you need to switch between orgs, this command gives you a quick reference. It helps ensure you’re using the correct org for your operations, avoiding potential mistakes during development or deployment.
3. Set default Salesforce Org
sf config set target-org <alias/username>
Description: This command is used to set the default Salesforce org (scratch org, sandbox, or production) for subsequent CLI operations. By specifying an alias or username, you configure the Salesforce CLI to use that org for all future commands without needing to pass the alias or username each time.
Use Case: This command is particularly useful when you work with multiple Salesforce orgs in your development environment. Instead of specifying the org alias or username with every command, you can set it once and all subsequent commands will use this target org automatically, saving time and reducing errors.
Example: This sets “my-sandbox” as the default org for future commands, such as deployments or data retrieval, so you don’t have to explicitly mention the org in each command.
sf config set target-org my-sandbox
Use Case Scenario: Suppose you’re developing in multiple environments (e.g., a scratch org, a sandbox, and a production org). After creating or authenticating an org, you can set the specific org as your target. This helps ensure that all commands such as sf deploy or sf retrieve automatically apply to the correct org without needing repeated org identifiers.
4. Logout from a Salesforce Org
sf org logout --target-org <target_org>
Description: This command logs you out of a specific Salesforce org that you have authenticated with the Salesforce CLI. You specify the target org either by its alias or username, and the CLI removes the authentication for that org, preventing further commands from interacting with it until you log back in.
Use Case: This command is useful when you no longer need to work with a particular Salesforce org and want to remove its authentication from your CLI environment. It helps in maintaining security by ensuring that org credentials are no longer active in your CLI, especially when working in shared environments or rotating through different orgs.
Example: This logs you out of the org with the alias “my-sandbox,” removing its credentials from your environment.
sf org logout --target-org my-sandbox
Use Case Scenario: Suppose you have been working with several scratch orgs or sandboxes, and one of them is no longer needed or has expired. By using this command, you can securely log out of the specific org, ensuring that no further commands are accidentally run against it. This is especially useful for security and organization when dealing with multiple Salesforce environments. It also helps prevent command errors by ensuring you only have active, relevant orgs authenticated in your CLI.
5. Deploy to Salesforce Org
sf project deploy start --source-dir <source_dir> --target-org <target_org>
Description: This command deploys specific source files or metadata from your local project directory to a target Salesforce org. You specify the directory containing the source files using --source-dir
and define the target org using --target-org
, either by alias or username. This command initiates the deployment process and pushes the selected components to the org.
Use Case: This command is ideal for deploying specific parts of your project (such as new features, bug fixes, or metadata) to a Salesforce org without deploying the entire project. It allows selective deployments based on the source directory, making it efficient for controlled releases or partial updates. This is particularly useful in environments where only certain parts of the code need to be
Example: This deploys the Apex classes from the force-app/main/default/classes
directory to the “my-sandbox” org.
sf project deploy start --source-dir force-app/main/default/classes --target-org my-sandbox
Use Case Scenario: Imagine you’re working on a large project, but only a small subset of files, such as Apex classes or Lightning components, needs to be deployed to a sandbox or production org. Rather than deploying the entire project, you can use this command to deploy only the necessary components. This makes the deployment process faster and minimizes the risk of deploying unwanted or incomplete features, which is crucial in both development and production environments.
6. Retrieve from Salesforce Org
sf project retrieve start --source-dir <source_dir> --target-org <target_org>
Description: This command retrieves specific metadata from a target Salesforce org and saves it in your local project directory. You specify the directory where the retrieved metadata should be saved using --source-dir
, and the target org from which to retrieve the metadata using --target-org
(identified by its alias or username).
Use Case: This command is useful when you need to fetch specific components or metadata from a Salesforce org, such as Apex classes, Visualforce pages, or Lightning components, without retrieving the entire org’s metadata. It enables more focused and efficient retrievals, especially when only a subset of metadata needs to be updated or reviewed in the local development environment.
Example: This command retrieves Apex classes from the “my-sandbox” org and saves them in the force-app/main/default/classes
directory locally.
sf project retrieve start --source-dir force-app/main/default/classes --target-org my-sandbox
Use Case Scenario: Suppose you’ve made changes directly in a Salesforce sandbox or production org (e.g., through the Salesforce UI) and now want to retrieve those changes to keep your local repository up-to-date. Instead of retrieving the entire org’s metadata, you can use this command to retrieve only the relevant components, such as specific classes or custom objects, reducing the time and complexity of the retrieval process. This is particularly useful in version control scenarios where you need to sync the latest metadata with your local project.
7. Open Salesforce Org
sf org open
Description: This command opens the default Salesforce org (or a specified org by alias or username) in your web browser. It logs you into the org directly, without needing to manually enter your credentials, making it a convenient way to access the Salesforce org from the CLI.
Use Case: This command is highly useful for quickly accessing a Salesforce org that you’ve authenticated to via the CLI. Instead of manually logging into the Salesforce org through a browser, the sf org open
command handles the login process for you. It saves time, especially when you need to frequently switch between different Salesforce orgs for development, testing, or deployment tasks.
Example: You can also specify an alias or username if you want to open a specific org:
sf org open --target-org my-sandbox
Use Case Scenario: Suppose you are working on multiple Salesforce orgs, such as a sandbox for testing and a production org for deployment. With this command, you can quickly open and access any org that has been authenticated through the CLI. This is especially useful when you’re switching between different orgs multiple times a day, and it ensures you don’t have to remember the login credentials for each org manually. Additionally, it helps you avoid accidentally opening the wrong environment during critical tasks like deployment or testing.
8. Query records from Salesforce Org
sf data query --query "<query>"
Description: The sf data query --query "<query>"
command executes a SOQL (Salesforce Object Query Language) query against the target Salesforce org. The query results are displayed directly in the terminal or can be saved in different formats if specified. This command allows you to retrieve data from Salesforce objects quickly using SOQL queries.
Use Case: This command is highly useful for developers or administrators who need to retrieve specific data from Salesforce objects without logging into the org and manually searching through records. By running a SOQL query through the CLI, you can instantly get results for debugging, reporting, or verifying data directly from your terminal. It’s especially effective for quick data checks during development or deployment processes.
Example: his SOQL query retrieves the Name
and Email
fields from all Contact
records associated with a specific AccountId
.
sf data query --query "SELECT Name, Email FROM Contact WHERE AccountId = '001xxxxxxxxxxxxxxx'"
Use Case Scenario: Imagine you are working on a development task and need to quickly verify that certain records exist in a Salesforce object or check their values. Instead of opening Salesforce, navigating to the object, and manually filtering the records, you can run a SOQL query using the sf data query
command to pull the specific data you need. This is especially helpful when verifying deployment changes, testing in different environments, or generating quick reports for analysis.
9. Create a record in Salesforce Org
sf data create record --sobject <object_api_name> --values "<space_separated_key_values>"
Description: This command is used to create a new record in a specified Salesforce object. You define the Salesforce object by its API name and provide field-value pairs as input. The command creates the record in the target org and returns the newly created record’s ID or success message.
Use Case: This command is particularly useful for quickly inserting new records into Salesforce without needing to log into the org or use the UI. It can be used to automate data creation tasks during development, testing, or even data migration processes. It’s ideal for developers who need to quickly create test data or for administrators who are automating repetitive data entry tasks.
Example: This command creates a new Account record in Salesforce with the Name
set to “Test Account” and the Industry
field set to “Technology.”
sf data create record --sobject Account --values "Name='Test Account' Industry='Technology'"
Use Case Scenario: Suppose you are developing an integration that requires a sample record to exist in the Salesforce database for testing purposes. Instead of manually creating the record in the UI, you can use this command to quickly generate the required data directly from the CLI. Additionally, this command is useful when testing automation workflows or processes, as you can create records on demand for verification. It also speeds up tasks like data migration, where you need to create multiple records programmatically.
10. Generate an Apex Class
sf apex generate class --name <class_name>
Description: The sf apex generate class --name <class_name>
command generates a new Apex class with the specified class name in your Salesforce project. The command creates a basic template for the class, which you can later customize with specific business logic. It saves time by automating the creation of an Apex class file, ensuring proper structure and naming conventions are followed.
Use Case: This command is ideal for developers who frequently create new Apex classes and want to streamline the process. Instead of manually creating the class file and setting up the necessary structure, the CLI automates the task, making it faster and more efficient. It’s particularly useful in larger projects where multiple classes need to be created during different stages of development.
Example: This creates a new Apex class called MyNewClass
with a basic template.
sf apex generate class --name MyNewClass
Use Case Scenario: Suppose you are starting a new feature that requires additional business logic in your Salesforce org, and you need to create several new Apex classes. Using this command allows you to quickly generate the class structure with the required naming convention, allowing you to focus on writing the business logic instead of setting up boilerplate code. This is also helpful in projects that follow strict development standards, as the auto-generated class structure ensures consistency.