Skip to main content

(Devops) How to use Visual Studio Code IDE for deploying in Salesforce?

 

How to use VS Code IDE for deploying in Salesforce?


The Visual Studio Code IDE was introduced by Microsoft in 2015. One of the main advantages with Visual Studio Code IDE is its in-built debugger which makes the development code flow easily without requiring multiple clicks and maintains a single view with code and debugger. This also means that the bug-tracking and code run-throughs are a lot easier and faster.

With the above advantages of VS Code, many developers and Devops teams are adopting this tool for development and deployment of code in Salesforce. I am going to walkthrough the process of setting up VS Code for Salesforce development and deployment as shown below:


Step 1: Download and Install VS Code

To download visual studio code , open the link https://code.visualstudio.com/ and click on "Download" button to download a specific version to your system. For example Windows or Mac version based on your OS. Although there are insider builds and online builds, I would recommend you to install a stable version to avoid any IDE bugs. However, if you want to explore latest features of VS Code, feel free to download the insider bugs which showcase latest features which are currently being tested but not released yet.


Step 2 : Download and Install Salesforce extensions pack

After installing VS code, I would recommend you to install Salesforce CLI Pack before installing the Salesforce extensions. The Salesforce CLI (Command Line Interface) pack enables you to control the full application lifecycle of salesforce apps. Through the command line instructions, it is easy to create create or connect to different environments used for development, testing, staging and synchronise metadata, code between your orgs and a version control system such as GitHub/VS Code.

The Salesforce CLI could be downloaded from the link as shown below:
 CLI - https://developer.salesforce.com/tools/sfdxcli

  • Once installation is complete, to confirm that the latest version of CLI is installed, please run the following command from the command line prompt.
           sfdx update 

  • You should see the output for this command as: sfdx-cli: Updating CLI... 

Now, it is time to install Salesforce extensions pack for vs code. The salesforce extension pack provides deep integrations with Salesforce tooling throughout VS Code. This extension pack installs the essential Salesforce extensions all at once, saving time instead of selecting each individual extension to install.

With Salesforce extensions you can invoke Salesforce CLI from various methods such as VS Code menus, shortcut keys and right click context menus. New functionality shows up as you are working with VS Code such as:

  • Language services for Visualforce pages and Apex Classes
  • Language services for Lightening components (Aura and Lightening web components)
  • Integrations in the activity bar for Apex tests and Replay Debugger.

1. Once you launch Visual Studio Code, click on 'Extensions' from the left hand toolbar. 

2. Enter 'Salesforce Extension Pack' in the search field.

3. Click on 'Salesforce Extension Pack'

4. Click on "Install"

5. Close and relaunch the Visual Studio Code for extensions to work.


Step 3:  Connecting to Salesforce Org

As you have now installed Salesforce CLI and all the required extensions, you could go ahead to connect your Salesforce org with VS Code IDE. Use command Ctrl + Shift + P or go to View -> Command Palette. In the command palette type :Create Project with Manifest and press 'Enter' button on the first option as shown below:




Select 'Standard' and enter the name of the project. For example, 'MyFirstProject'. The project will be created and the menu will appear on the left hand side as shown below:



Once the project is created, we need authorize the salesforce org. In order to do this, click on Ctrl + Shift + P or go to View -> Command Palette. In the command palette type: Authorize an org and press 'Enter' button on the first option as shown below:



We now need to select the environment we want to authorize such as sandbox environment or production environment as shown below:



I have a ready sandbox environment which is a mirror image of production and once I select this environment, the vs code editor would prompt for Org alias name e.g. MyFirstProjectOrg (similar to the name of my project). After pressing Enter button, it will open my default browser prompting me to enter login details. After logging in with your credentials, the Salesforce CLI would request permissions to open your org. Once the Salesforce CLI is able to authorise your org, you would see the commands as shown below:


Step 4: 

Once your org is successfully authorised, you may want to get all the Apex classes, trigger and other required files into the IDE. In order to do this, open the package.xml  from the manifest subfolder as shown in the left panel.  Once you right click on the package.xml and select the option SFDX: Retrieve Source in Manifest from Org as shown below:


Step 5:

Once your org is authorised, you could proceed to create an Apex Class. Click on the Explorer in Visual Studio code to expand the force-app folder. Next click on the VSCODEQUICKSTART directory, click force-app folder to show the full list of folders. Under the force-app/main/default directory, you find the metadata included within project such as applications, aura, classes and many more.  

Open the command palette from View -> Command (Ctrl/Command + Shift + P), type SFDX: Create Apex Class. Type the class name as "AccountController".

Navigate to the folder force-app/main/default classes as the directory where you would want to add the folder "AccountController.cls" class. Now open the class in the editor and replace the default code with the following code:

Public with sharing class AccountController {

  public static List <Account> getallActiveAccounts() {

     return { SELECT Id.Name_Active_c from Accounts where Active_c = "Yes" };

   }

}

Run the above SOQL query in your org before deploying your org. We use the command palette to run this query and deploy this code in your org by typing SFDX: Execute SOQL  query with currently selected text and press Enter. 

Select REST API and press Enter. In order to deploy your org, open command prompt Command + Shift + P for Mac or Ctrl + Shift + P for Windows OS and type SFDX: Deploy Source to Org.  In the output tab of the integrated terminal, the command SFDX: Deploy Source to Org ...ended with exit code 0. This means that the deploy command ran successfully. 





You have now deployed your code into Salesforce using VS Code! 




Comments

Popular posts from this blog

(Insights) What is Automation? What are the key areas where automation provides a value add?

 (Insights) What is Salesforce Automation? What are the key areas where automation provides a value add? As organisations need to scale businesses, manually repeating tasks are costly and time consuming. Once automation is implemented to automate these manually repetitive tasks for business processes, they can increase productivity and reduce time and save money. A decent CRM system with automation that orchestrate sophisticated processes will provide a solid foundation for business growth. According to Gartner’s Magic Quadrant on CRM  report  , the future of automation will play a significant role in organisation’s business strategy. Long term road maps are including automation to gain insight on the automation capabilities competitors will be using. In this article, we will discuss what is salesforce automation and key areas where automation is more effective and why is this important for organisations to implement in their salesforce org. What is Salesforce Automation?...

What's new with Record-Triggered Automation in Salesforce?

  What’s new with Record-Triggered Automation in Salesforce?  Systems demonstrate automated behavior by enabling business processes to scale and meet key goals and objectives quickly. When automation is implemented correctly, it eliminates manual and repetitive tasks allowing users to focus more on high value tasks. Most often automation means translating business processes from one form to another, for example paper based form to digital forms and with every change comes an opportunity for transformation. Record-Triggered automation is part of Flow Architecture where this automation focuses on trigger order of execution for Salesforce Records. Salesforce has introduced a new concept of low-code automation with the Flow to allow for significantly better performance when it comes to same-record field updates. However, additional complexity will be encountered by users who are unfamiliar with triggers and find this terminology confusing. So throughout this article, I will conti...

(DevOps) What is Salesforce Genie Real-time Data Platform? How does this empower Salesforce Customer 360?

  (DevOps)  What is Salesforce  Genie Real-time Data Platform? How does this empower Customer 360 Platform? Salesforce, the global leader in CRM, on their 20th annual event unveiled  Salesforce Genie , a hyper-scale  real-time data platform that powers the entire Salesforce Customer 360 platform . With Genie, every company can turn data into customer magic, delivering seamless, highly personalised experiences across sales, service, marketing, and commerce that continuously adapt to changing customer information and needs in real time. source: salesforce.com What is Salesforce Genie Platform? Customer 360 platform is an integrated CRM Platform that captures customer data from every step of its journey which is captured at one place (data lake). It enables different teams to work together by unifying analytics, sales, services, commerce and IT under a single CRM platform. Salesforce Genie platform is a new functionality empowers the existing Customer 360 Platform....