Create Downloadable Salesforce Packages with VS Code & CLI

To create a package in Salesforce, we use the package manager that adds components and generates an installation URL after uploading the package. This is a very easy way to create and deploy a package in Salesforce, allowing others to install it directly.

The issue arises when the created package is deleted from the source org. As a result, a user from another org attempting to install the package will be unable to access it, as it has been deleted from the source org.

To use the package in the target org, even if it is deleted from the source org, we need to download it in a zip file. Then, even if the source org deletes the data, other users can still install the package using the downloaded zip file.

In this article, we will explain how to create downloadable Salesforce packages with VS Code & CLI.

Create Package Using VS Code + Salesforce CLI

There are two ways to retrieve components from the Salesforce org to create the package, which we can deploy in the target org, even if those components are deleted from the source org

  1. Workbench: We can create and deploy a package using the Workbench in Salesforce.
  2. VS Code + Salesforce CLI: To use VS Code, the Salesforce CLI must be installed in VS Code.

Before creating the package, you must have installed VS Code and the Salesforce CLI.

Next, I’ll walk you through creating a project, generating a package.xml file, and converting it to a zip file using VS Code and Salesforce CLI. Follow the steps below.

1. Create a Salesforce Project and Folder

To create a project from a command, click the View menu and then select the Command Palette option.

Alternatively, you can press Ctrl+Shift+P to open the command bar. In the command, you need to search for and select SFDX: Create Project With Manifest.

Then, you need to select the Project Template. Here, I selected the Standard Template and pressed the Enter key.

Create Project in Salesforce

After selecting the template, you have to enter the Project Name and press the Enter key.

Setup VS Code for Salesforce Org

Now, we need to select a Folder so that we can save our project. To do that, select an existing folder or create a new folder and click the Create Project button.

How to Setup VS Code for Salesforce Org

2. Connect VS Code to Salesforce Org

Now, we need to connect VS Code to the Salesforce Org. For that, again, open the Command Palette. In the command, you need to search for and select SFDX: Authorize an Org.

Then, you need to select the default org that you are using. Here, I’m using Production org. If you have logged in through the URL to your org, then you can select Project Default or the Production org option.

Setup Vs Code and Connect to Salesforce Org

After that, you will be redirected to the Salesforce Org login screen. Here, you need to enter credentials, or you will get a confirmation screen asking if you want to allow the Salesforce Org to connect to VS Code. There, you need to click the Allow button.

How to Setup Vs Code and Connect to Salesforce Org

After that, you will be connected to the Salesforce Org using VS Code and receive an Authentication Successful message.

Connect VS Code to Salesforce Org

3. Retrieve Component from Salesforce Org to the VS Code

Now, we need to fetch the apex classes and VF pages created in the Salesforce Developer Console. Navigate to the Package.xml file and open it in the code editor.

Then, right-click on the opened code. A pop-up window will open, and in that window, select SFDX: Retrieve Source in Manifest from Org.

How to Connect VS Code to Salesforce

After that, you will see all the apex classes and VF pages that you have already created in the Salesforce org and will be connected to them.

Retrieve Data From Salesforce Org

4. Add Component to Package.xml File

To retrieve the component from the Salesforce org, we need to add the component to the package.xml file, which is located under the manifest option.

You can create a different XML file or edit the existing package.xml file as I showed below. Add the components that you want to retrieve from the Salesforce org.

For example, I want to create a package for the flow that I created to add CC recipients dynamically in Salesforce flow.

This flow is dependent on the custom object named CC Recipients__c. In this object, I stored the email addresses in the email fields.

If the Flow references Custom Objects, Email Templates, or Custom Fields, they must also be included in package.xml; otherwise, deployment may fail.

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">

<types>
    <members>Add_CC_List</members>
    <name>Flow</name>
</types>

<types>
    <members>CC_Email_List__c</members>
    <members>Apply_Leave__c</members>
    <name>CustomObject</name>
</types>

    <version>64.0</version>
</Package>

You can see we have added a component to the package.xml file. Then, to create the zip file, you need to run the following command:

sfdx force:mdapi:retrieve -r ./mdapi_output -k ./manifest/package.xml -u SourceOrg --wait 10

Using this command, you can retrieve the metadata (component), create a zip file named unmanged.zip (by default), and create a folder named mdapi_output to store the zip file.

In the image, you can see the folder path where we stored the zip file.

Create Package Using VS Code and Salesforce CLI

In the image below, you can also see that the zip file has been created in our system. You can share this file with other users who have a Salesforce org account.

Unmanaged Package Zip File in Salesforce

In this way, we can create the downloadable package.xml zip file, which contains the component that you wanted to deploy on the different Salesforce orgs.

Deploy Package to Different Salesforce Org Using VS Code + CLI

Now we will deploy this package to other Salesforce orgs. Before deploying to another org, let me show you the components we added in the above package that are not available in this org.

Below, you can see there are no components available in this org. Now we will install the package that we created.

Salesforce Object Manager

Next, open VS Code and log in to connect with the new Salesforce org where we will deploy the package.

Next, add the unpackaged.zip file to the project folder you created, as shown below.

You can see I added the package file to the project by creating a new folder named mdapi_output. This folder will display in VS Code under the project.

Deploy Unmanaged Package uing VS Code and Salesforce CLI

On the left side, you’ll find the project folder, which contains the mdapi_output folder with the zip file.

Next, you can log in using the command or from the Command Palette. In the command, you need to search for and select SFDX: Authorize an Org. to connect VS Code to the Salesforce org, as given below:

sfdx auth:web:login -a TargetOrg

Here, the TargetOrg is our alias to verify the org where we are going to deploy the package.

After that, we need to provide a command to deploy the package as given below:

sfdx force:mdapi:deploy -u TargetOrg -f ./mdapi_output/unpackaged.zip -w -1

Using this command, the package will get deployed in this org. After that, you can see the Deploy ID and the Components that have been deployed.

Create Downloadable Salesforce Packages with VS Code &amp; CLI

To check the installed components, open your target (in which org you deployed the package). As you can see, earlier we couldn’t find the components, but after deploying the package, we can see the components (metadata) that we retrieved from the source org.

Deployed Unmanaged Package in Salesforce

In this way, we can deploy the unmanaged package by installing a ZIP file in Salesforce.

Conclusion

I hope you have got an idea about how to create downloadable Salesforce packages with VS Code & CLI. 

In this, I have explained how to create a package.xml file in which we can add the components that we want to deploy in different orgs. We then learned how to deploy a package downloaded as a ZIP file using VS Code and Salesforce CLI.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.