How to use the command "sf" (with examples)
The “sf” command is a powerful command line interface that simplifies development and build automation when working with your Salesforce org. It provides a set of commands that allow you to authorize orgs, manage metadata, generate passwords, assign permission sets, and more.
Use case 1: Authorize a Salesforce Organization
Code:
sf force:auth:web:login --setalias organization --instanceurl organization_url
Motivation: Authorizing a Salesforce Organization is the first step in working with the Salesforce CLI. This command allows you to authenticate and obtain access to the desired organization.
Explanation:
sf
: The Salesforce CLI command.force:auth:web:login
: The specific command to authorize a Salesforce Organization using web-based authentication.--setalias organization
: Sets an alias for the authorized organization, which can be used when referencing the organization in subsequent commands.--instanceurl organization_url
: Specifies the URL of the Salesforce instance to be authorized.
Example output:
Successfully authorized organization with alias 'organization' and instance URL 'organization_url'.
Use case 2: List all authorized organizations
Code:
sf force:org:list
Motivation: Before performing any operations on Salesforce orgs, it is useful to know which orgs are currently authorized. This command provides a list of all authorized organizations along with their aliases.
Explanation:
sf
: The Salesforce CLI command.force:org:list
: The specific command to list all authorized Salesforce Organizations.
Example output:
Organization Alias Username Connected Type
──────────────────────────────── ─────────── ────────────── ─────────── ──────────
My Organization org1 user1@company Yes Production
Another Organization org2 user2@company Yes Sandbox
Use case 3: Open a specific organization in the default web browser
Code:
sf force:org:open --targetusername organization
Motivation: Opening a specific Salesforce organization in the default web browser is useful when you need to quickly access the organization’s setup or user interface.
Explanation:
sf
: The Salesforce CLI command.force:org:open
: The specific command to open a specific Salesforce Organization in the default web browser.--targetusername organization
: Specifies the target organization by its alias or username.
Example output: The specified organization is opened in the default web browser.
Use case 4: Display information about a specific organization
Code:
sf force:org:display --targetusername organization
Motivation: It is often necessary to quickly retrieve information about a specific Salesforce organization, such as its ID, type, or connected status. This command provides an overview of the specified organization.
Explanation:
sf
: The Salesforce CLI command.force:org:display
: The specific command to display information about a specific Salesforce Organization.--targetusername organization
: Specifies the target organization by its alias or username.
Example output:
Username: user1@company
Org Id: 00DXXXXXXXXXXXXXXX
Instance Url: https://instanceurl.lightning.force.com
Connected: Yes
Use case 5: Push source metadata to an Organization
Code:
sf force:source:push --targetusername organization
Motivation: When working with source control and version control systems, it is important to synchronize changes between your local source code and a Salesforce organization. This command pushes the local source metadata to the specified organization.
Explanation:
sf
: The Salesforce CLI command.force:source:push
: The specific command to push source metadata to a Salesforce Organization.--targetusername organization
: Specifies the target organization by its alias or username.
Example output:
Pushed metadata to organization with alias 'organization'.
Use case 6: Pull source metadata from an Organization
Code:
sf force:source:pull --targetusername organization
Motivation: Pulling source metadata from a Salesforce organization is necessary when you want to retrieve the latest changes made in the organization and update your local source code accordingly.
Explanation:
sf
: The Salesforce CLI command.force:source:pull
: The specific command to pull source metadata from a Salesforce Organization.--targetusername organization
: Specifies the target organization by its alias or username.
Example output:
Pulled metadata from organization with alias 'organization'.
Use case 7: Generate a password for the organization’s logged-in user
Code:
sf force:user:password:generate --targetusername organization
Motivation: Generating a password for a user in a Salesforce organization is necessary when the user faces login issues or when setting up a new user.
Explanation:
sf
: The Salesforce CLI command.force:user:password:generate
: The specific command to generate a password for the organization’s logged-in user.--targetusername organization
: Specifies the target organization by its alias or username.
Example output:
Generated password for user 'user1@company' in the organization with alias 'organization':
New Password: Xxxxxxxxxxxx
Use case 8: Assign a permission set for the organization’s logged-in user
Code:
sf force:user:permset:assign --permsetname permission_set_name --targetusername organization
Motivation: Assigning a permission set to a user in a Salesforce organization allows them to access and perform specific actions as defined by the permission set.
Explanation:
sf
: The Salesforce CLI command.force:user:permset:assign
: The specific command to assign a permission set to the organization’s logged-in user.--permsetname permission_set_name
: Specifies the name of the permission set to assign to the user.--targetusername organization
: Specifies the target organization by its alias or username.
Example output:
Assigned permission set 'Permission Set Name' to user 'user1@company' in the organization with alias 'organization'.
Conclusion:
The “sf” command provides a set of useful features to simplify Salesforce development and build automation. With the various use cases covered in this article, you now have a good understanding of how to use the “sf” command for authorizing orgs, managing metadata, generating passwords, assigning permission sets, and more.