SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel
Showing posts with label SFDX Deploy Tool. Show all posts
Showing posts with label SFDX Deploy Tool. Show all posts

Tuesday, 30 June 2020

Extract Update and Deploy Metadata to Salesforce Org | SFDX Deploy Tool | Deploy Extracted Metadata

Hello Trailblazers,

In this tutorial, we're going to learn how we can very easily extract, update and then deploy metadata to a Salesforce Org by executing a single command from SFDX Deploy Tool for Windows.

Use Case:- In real life projects, when we're actually working in Salesforce, it usually happens that we need to deploy some metadata from one Salesforce Environment to another, let's say you've completed a feature in developer environment and you need to push all the changes to a QA sandbox, while pushing the changes it usually happens that we face some deployment issues, and we need to update some metadata and then try to deploy again and again. This can be very hectic specifically if you're using change sets which take time to upload and then you need to wait for it to be available in the destination org where you want to deploy.

We'll learn how we can make those deployments very easy using SFDX Deploy Tool for Windows

SFDX Deploy Tool can be downloaded from the github repository here:- https://github.com/rahulmalhotra/SFDX-Deploy-Tool

For this tutorial, we need SFDX Deploy Tool to be already setup with you. If you want to know more about how to setup SFDX Deploy Tool, have a look at the README file of the GitHub repository or have a look at this blog post or the tutorial below:-


Once you've setup SFDX Deploy Tool, i.e. the source and destination orgs, where you have to pull and push the metadata respectively. You can open the tool and you'll see the below screen:-


As you can see in the above image, I have updated this tool and added 3 more options at number:- 2, 5 and 6. These options are:-
  • Extract fetched metadata at option 2
  • Validate extracted metadata in destination org at opton 5
  • Deploy extracted metadata in destination org at option 6

Once you've setup the tool, the first step you need to do is to fetch the metadata from the source org. That you can do by setting up the package.xml present under the metadata folder. After you have the package.xml ready, open the deploy tool, choose option 1 and press the Enter key. A sample package.xml is shown below:-


Once you've fetched the metadata from the source org, you'll see an unpackaged.zip file automatically created in the base folder as shown in the above image. This zip file mainly consists of all the metadata that you've fetched using the package.xml. Our next step is to extract and update this metadata before deploying it to the destination org.

To extract the metadata, open SFDX Deploy Tool and choose option 2 with the name:- Extract fetched metadata this option will extract the fetched metadata using windows powershell, once the metadata is successfully extracted, you'll see an output as shown below:-


In the base folder, you can see that the unpackaged.zip file is extracted automatically and a new folder named unpackaged is created which consist of all the metadata that we fetched from the source org as shown below:-



In case you don't have powershell in your windows you can use any other tool to extract that zip file. Now, we can simply open any metadata and update it. Once, we've updated the metadata, it's time to validate and deploy the extracted metadata.

To Validate Extracted Metadata:- Open SFDX Deploy Tool and choose option 5 with the name:- Validate extracted metadata in destination org

To Deploy Extracted Metadata:- Open SFDX Deploy Tool and choose option 6 with the name:- Deploy extracted metadata in destination org. This option will automatically pick the updated files in the extracted folder and deploy them to your destination org.

If you face errors while deployment, you can simply update the extracted files in VSCode and use SFDX Deploy Tool to deploy the metadata again and again in a single command.

Tired of reading or just scrolled down ? Don't worry, you can watch the video too:-



That's all for this tutorial everyone. I hope you liked it, give a try to SFDX Deploy Tool and let me know your feedback in the comments down below.

Happy Trailblazing..!!

Sunday, 23 June 2019

SFDX Deploy Tool - Easily deploy metadata using sfdx cli

Introducing SFDX Deploy Tool, a user friendly salesforce deployment tool which can be used to deploy metadata from one salesforce org to another.


Installation

To use this tool, you must have SFDX CLI installed in your system. You can download Salesforce CLI from here. 

Once you've downloaded and installed SFDX CLI, the next step is to clone the SFDX-Deploy-Tool repository from github. You can also download it as a zip file by clicking here. You can also use the below command to clone the repository:-

git clone https://github.com/rahulmalhotra/SFDX-Deploy-Tool.git

Once you've extracted this tool. You'll see a config.txt file with the information as shown below

sourceOrg=CR1 // Sfdx cli alias of source org
destinationOrg=PB1 // Sfdx cli alias of destination org
package.xmlLocationToDeploy=metadata\package.xml // Package.xml path
folderLocationForFetchedZip=metadata // Folder to save zip file of code fetched from source org
waitTimeInMinutes=10 // Deployment wait time in minutes
testLevel=RunSpecifiedTests // Specify whether to run test cases or not
runTests=TemperatureConverterTest,LeadProcessorTest // Comma separated names of test classes
folderLocationToUndeploy=metadata\destructive // Location of folder containing destructiveChanges.xml and empty package.xml used in case of un-deploy metadata from destination org

Follow the below steps to configure config.txt :-
  1. Replace CR1 by the alias of your source org in sfdx cli ( If you haven't connected your source org using sfdx cli in past, you can delete CR1 and leave this space empty )

  2. Replace PB1 by the alias of your destination org in sfdx cli ( If you haven't connected your destination org using sfdx cli in past, you can delete PB1 and leave this space empty )

  3. As you can see, the package.xmlLocationToDeploy is given the value metadata\package.xml i.e. the package.xml inside the metadata folder will be used to fetch and deploy data. Either you can update the package.xml inside the metadata folder with your own metadata or you can specify the path to your own package.xml in the config file.

  4. You can also see that the folderLocationForFetchedZip is given the value metadata. This means that when you'll fetch the metadata from source org it'll create a zip file inside the metadata folder and this zip file will be used later to deploy the metadata to destination org. If you want to use a different folder, you can specify the full path to that folder in config file.

  5. Now we have the waitTimeInMinutes which is specified as 10 minutes you can update it according to your choice.

  6. SFDX CLI provides you 4 test levels namely:- NoTestRun, RunSpecifiedTests, RunLocalTests and RunAllTestsInOrg. I have given my testLevel parameter the value of RunSpecifiedTests which will use the runTests parameter to get the test classes name which will be executed while deployment. You can also change it to NoTestRun or anything else from the available test levels but make sure that for the runTests parameter, you're specifying the testclass names separated by ",". The runTests parameter will be used only when the testLevel is set to RunSpecifiedTests.

  7. The folderLocationToUndeploy should consists of the location of a folder which contains an empty package.xml file and a destructiveChanges.xml file. This will be used when you need to un-deploy or remove metadata from the org. The metadata specified in destructiveChanges.xml will be removed from destination org.

SFDX Deploy Tool is now ready for use.

Usage

You just need to make sure that the file sfdxdeploytool.bat and config.txt are in the same folder. If you're using the package.xml in the metadata folder and the destructiveChanges.xml which is present in the metadata/destructive folder, keep the folder structure as it is. After configuring, double click on sfdxdeploytool.bat and you're good to go.

In case, you've not specified alias for source or destination org, when you open sfdxdeploytool.bat you'll be asked to enter a new alias and also the instance url which is the login url for your org. In the login url, you can add the url according to the developer or sandbox edition or even a custom url.


Your default browser will be opened using which you can login into your org and authorize the sfdx cli to use your org. The config.txt file will be updated automatically as you authorize your org. Once you've your source and destination org properly setup along with the config.txt file, you'll see the below menu:-

1. Fetch metadata from source org

If you select the first option, it'll fetch the metadata according to the definition in package.xml whose path is given by:- package.xmlLocationToDeploy in config.txt. It'll create a zip file and store it in the folder specified by folderLocationForFetchedZip in config.txt.


In my case, the zip file is created in the metadata folder named unpackaged.zip as shown below:-


It'll consist of 4 apex classes which are specified in my package.xml and are present in my source org.


You can perform more operations by entering y once the operation is performed or can enter n to exit the tool.

2. Validating metadata in destination org

If you press option 2 in the main menu, the retrieved zip file will be validated in the destination org as shown below:-


In this case, no metadata will be deployed to the destination org, it'll only be validated. If you see the config file, I have specified the testLevel as RunSpecifiedTests and also specified two test classes in the runTests as:- TemperatureConverterTest,LeadProcessorTest. Therefore, my test classes are run during validation.

As you can see below, my code is validated successfully in the destination org and I have specified y at the end as I want to perform more operations.


I can search for deployment status in my destination org and view on the deployment status page that my metadata is validated successfully along with the test cases as shown below:- 

3. Deploying metadata in destination org

If you press option 3 in the main menu, the retrieved zip file will be deployed in the destination org as shown below:-


As you can see, all the test cases are run again and code is deployed in the destination org:-


While deploying, I can also go to the deployment status page of my destination org to see the deployment status as shown below:-

 
As you can see above, the deployment is successful in the destination org. Now the last option we have is to un-deploy metadata in the destination org.

4. Un-deploying metadata in destination org

In order to remove metadata from the destination org, I have specified the folderLocationToUndeploy in config.txt file. In my case, I am using the destructive folder present inside the metadata folder. The folder you'll specify for destructive deployment must consists of an empty package.xml and a destructiveChanges.xml file which will consists of the metadata definition we need to un-deploy.

Below is the package.xml present inside my destructive folder:-


And the destructiveChanges.xml which consists of 4 apex classes that I deployed to the destination org as I'll be removing the same.


When, I choose the 4th option from the main menu in my SFDX Deploy Tool, it'll automatically un-deploy the metadata mentioned in destructiveChanges.xml file as shown below:-


Similarly, if I check the deployment status in my org:-


It says deployment succeeded but, in actual the metadata is un-deployed or removed from the destination org. In case of un-deployment, no apex test classes are needed to run as we're doing destructive deployment.

So, that's all for this post. I hope you liked it and also liked the SFDX Deploy Tool. If you have any doubts, post them down in the comments and make sure to show your support by giving a star to the github repository here:- https://github.com/rahulmalhotra/SFDX-Deploy-Tool

You can also contribute to this repository, all the guidelines are mentioned in the CONTRIBUTING.md file and you can also tweet about this tool by using the twitter button in the README.md file at github.

Happy Trailblazing..!!