How to authorize salesforce org from VS Code?

Last Updated : 28 Jul, 2025

Salesforce development has evolved significantly, with Visual Studio Code (VS Code) emerging as the go-to integrated development environment (IDE) for developers. It offers powerful features like syntax highlighting, version control, and seamless integration with Salesforce. One of the essential tasks for Salesforce developers is authorizing a Salesforce org in VS Code to streamline development workflows.

This guide will walk you through the detailed process of authorizing a Salesforce org in VS Code, tailored for intermediate to advanced developers.

Prerequisites

Before proceeding, ensure the following tools are installed and set up on your machine:

  • Visual Studio Code: Download and install VS Code.
  • Salesforce CLI (SFDX): Download and install the Salesforce CLI. Verify the installation by running the command:
sfdx version
  • Salesforce Extension Pack: Install the "Salesforce Extension Pack" from the VS Code marketplace. This includes essential extensions for working with Salesforce, such as Apex, Visualforce, Lightning Web Components, and Salesforce CLI Integration.

Step-by-Step Guide to Authorizing a Salesforce Org

1. Set Up the Salesforce DX Project

Before authorizing an org, you need a Salesforce DX project. To create one:

  • Open VS Code.
  • Press Ctrl + Shift + P (or Cmd + Shift + P on macOS) to open the Command Palette.
  • Type and select SFDX: Create Project with Manifest.
  • Provide a name for your project and choose the location to save it.
  • Once the project is created, you’ll see a basic folder structure in VS Code.

2. Authorize a Salesforce Org

Now that the project is ready, follow these steps to connect to your Salesforce org:

Step 1: Open Command Palette

Press Ctrl + Shift + P (or Cmd + Shift + P on macOS) and type SFDX: Authorize an Org.

Step 2: Select the Environment

You’ll be prompted to select the type of Salesforce environment you wish to connect to:

  • Production: For standard production or Developer Edition orgs.
  • Sandbox: For connecting to a sandbox environment.
  • Custom URL: For custom domain environments (e.g., mydomain.my.salesforce.com).

Step 3: Provide an Alias

Enter a meaningful alias for your org. For example:

dev-org

This alias helps you identify the org when working on multiple projects or switching between environments.

Step 4: Authenticate via Browser

A new browser tab will open, prompting you to log in to your Salesforce org. Use your credentials to authenticate. Once logged in, Salesforce will ask you to allow access. Click Allow.

Step 5: Confirm Authorization

After successful authentication, you’ll be redirected back, and VS Code will display a confirmation message that the org has been authorized.

3. Verify Org Authorization

To ensure the Salesforce org is connected:

  • Run the following command in the VS Code terminal:
sfdx force:org:list
  • You’ll see a list of all authorized orgs, including their aliases and login URLs.

Example output:

Alias       Org ID              Instance URL                           Status
────────── ──────────────── ───────────────────────────────────── ──────
dev-org 00DXXXXXXXXXXXXXX https://ab1752821344739.my.salesforce.com/ Active

4. Retrieve Metadata from the Org

Once the org is authorized, you can retrieve metadata to start working on your project:

  • Open the Manifest folder in your project and locate the package.xml file.
  • Update the package.xml file to specify the components you want to retrieve.
  • Right-click on the package.xml file and select:
SFDX: Retrieve Source from Org
  • This action retrieves the specified metadata and populates the project structure.

5. Deploy Metadata to the Org

To deploy changes back to the Salesforce org:

  • Make your changes to the desired files (e.g., Apex classes, Lightning components).
  • Right-click on the file or folder and select:
SFDX: Deploy This Source to Org
  • Confirm the deployment status in the terminal. Errors, if any, will be displayed with detailed messages.

Best Practices

  1. Use Aliases: Always assign a unique alias to your orgs when authorizing them. This makes it easier to switch between orgs during development.
  2. Secure Your Credentials: Avoid sharing your credentials or tokens. Use environment-specific aliases to safeguard against accidental deployments.
  3. Keep Salesforce CLI Updated: Regularly update the Salesforce CLI using the sfdx update command to ensure compatibility with the latest Salesforce features.
  4. Leverage Source Control: Use Git or another version control system to track changes and collaborate with your team effectively.

Troubleshooting Common Issues

  1. Authorization Error: If the browser doesn't open automatically, check your system's default browser settings or copy-paste the URL from the terminal manually.
  2. Connection Timeout: Ensure that your network is stable and not blocking Salesforce domains.
  3. Unable to Retrieve Metadata: Double-check the package.xml file for typos or unsupported metadata types.
  4. Invalid Org Type: Ensure you select the correct environment (Production, Sandbox, or Custom) when authorizing the org.

Conclusion

Salesforce development has evolved significantly, with Visual Studio Code (VS Code) emerging as the go-to integrated development environment (IDE) for developers. It offers powerful features like syntax highlighting, version control, and seamless integration with Salesforce. One of the essential tasks for Salesforce developers is authorizing a Salesforce org in VS Code to streamline development workflows.

This guide will walk you through the detailed process of authorizing a Salesforce org in VS Code, tailored for intermediate to advanced developers.

Comment