Call us: +1 914 933 7633

Salesforce Deployment by using SFDX

Salesforce Deployment by using SFDX
May 24, 2020 Rahul Ramakrishnan

Salesforce Deployment by using SFDX

If we are using an SFDX CLI tool we can easily retrieve and deploy data as a metadata API format. In Salesforce, currently there are lots of deployment tools. Some of those tools are Data Migration Tool, Change Set, Force.com IDE, and so on. In this blog, I will be mentioning the methods to retrieve and deploy metadata API files into another org. This is equivalent to a deployment between the Orgs.

Create a New Project

First we need to learn how to create a project by using SFDX. Check out the below steps and learn the steps to create a new project.

Step 1:

Open VS Code and click, SHIFT+CTRL+P, and then SFDX: Create New Project With Manifest.

Step 2:

Choose Standard project template.

Step 3:

Give a meaningful name for your project.

Step 4:

Choose the drive and folder you want to save your work

Step 5:

Now your project creation is completed. If you check the folder, you can see all the related folders. The next step is authenticating the Org.

How to Authenticate Your Org

If we complete the authentication successfully, we will get an active connection between DX and our Salesforce Org. Authentication can be done in two ways by using SFDX: Authorize Org command directly from the command box, or we can use SFDX command in command terminal.

sfdx force:auth:web:login

We can use many attributes with this command,

sfdx force:auth:web:login -a sfdev -s –r https://login.salesforce.com

-a :- We can set an alias for our authenticated org
-s :- If you want you can set a default user name
-r :- Instance URL

**To connect sandbox we can use https://test.salesforce.com and for live https://login.salesforce.com

Step 1:

Just open the terminal and use the above authentication command.

Step 2:

Now your default browser will open for entering your Salesforce org credentials.

Step 3:

Once the login is successful, your org is successfully connected with SFDX.

How to Create a Class and Deployed into a Salesforce org

Once your connection is succeeded, you can create a class and deploy it in the connected organization. Next, let’s take a look at the steps for creating a new sample class and deploying it.

Step 1:

Press SHIFT+CTRL+P and then choose SFDX: Create Apex Class.

 

Step 2:

Give a meaningful name for your class and click Enter.

Step 3:

Now the platform is ready for coding. Once you complete your coding, you can deploy it into your connected org.

Step 4:

To deploy our code to connected org:

4.1 Open the class folder from the explorer (Inside force-appmaindefault).

4.2 Right-click on the class you want to deploy.

4.3 Click SFDX: Deployee Source to Org

4.3 Then the code is successfully deployed into the connected org.

How to Retrieve Metadata API and Get it Deployed to Another Org

For deploying components from one organization(sandbox) to another organization(production), as a first step, we have to retrieve all the components we need in the metadata API format.

Step1:

Add components in package.xml file. I just want to deploy classes and custom objects, so I have to add this component into package.xml file

  1. A) Open package.xml file (force –appmaindefault -> manifest ->package.xml)

Step 2:

To retrieve metadata, we can use the command:

sfdx force:mdapi:retrieve
sfdx force:mdapi:retrieve -u sfdev -r . -k package.xml

Few command attributes are

-u : targetusername
-r : –retrievetargetdir
-k : –unpackaged

Once it completes, all the components we specified in package.xml gets retrieved into the root folder, which we selected in the time of project creation.

Once the retrieve is complete, we can check the manifest folder in our local system, to view the retrieved components.

Just extract the folder and we can check the components. If something is required to be changed or removed, we can do this.

Authorize your second org (production or where to deploy the code)

You can use same authorization method which I mentioned above. Here I am trying to deploy into the other sandbox org (sfdev to sfqa). I will login to the other sandbox org by using auth command and another connection alias name.

Step 1

sfdx force:auth:web:login -a sfqa -s -r https://login.salesforce.com

Once the authentication is a success, the connection name should be changed to a new alias name (in this case sfdev changed to sfqa).

Step 2:

Now it connected to our second org and we can deploy the components we have prepared.

Command used to deploy metadata

sfdx force:mdapi:deploy 

We can use attributes with command some attributes are

-u: checkonly
-d : deploydir

Example: sfdx force:mdapi:deploy -u sfqa -d .

Once the deployment is completed you can check the deployment status in Salesforce (Setup -> Deployment Status) or you will get deployment report by using the command (sfdx force:mdapi:deploy:report) also.

**** In the above examples I am showing the deployment of custom objects and classes. You can use this method to deploy whatever you want to. However, one condition is that you need to check and ensure the component is included in the package.xml File.

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*