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

Sunday 9 June 2019

How to setup Visual Studio Code for Salesforce ?

Visual Studio Code is the latest IDE for Salesforce Development and in this blog post, I am going to tell you how you can setup Visual Studio Code for Salesforce Development. So, let's begin.

Download and Install VSCode

To download Visual Studio Code to your system, just go to:- https://code.visualstudio.com/ and you'll see an option to download VSCode for your system. I recommend you to download the stable build which is usually free from bugs, however, if you're keen to get the latest upcoming features before the world, you can download the insiders edition too.


Download and install VSCode in your system and on opening, you'll see the welcome screen as shown below:-

Download and Install Salesforce DX CLI

Now, as we've installed VSCode in our system, the next step is to install Salesforce DX CLI. To install that, go to:- https://developer.salesforce.com/tools/sfdxcli and you'll see the page as shown below from where you can download the salesforce dx cli and install the same on your system.


Once you have sfdx cli installed, you can test it by opening the command window and typing sfdx and then pressing enter you should get the result similar to the one given in the below image:-


Cool. You've now properly installed Salesforce DX CLI in your system. Let's get to the next step.

Install Salesforce Extension Pack

In order to make our IDE Salesforce ready. Salesforce has made it's own extension pack available for developers. Once you open your VSCode IDE you can click on the 4th icon on the left hand sidebar menu which is for managing extensions. Search for Salesforce Extension Pack and click on the first search result as shown in the below image:-


I've already installed that in my VSCode IDE so it's giving me the option to disable or uninstall it however you'll see a green button with install written on it as shown in the other search results in the image, just click on that button and it'll install the Salesforce Extension Pack for you. Salesforce Extension pack consists of a number of extensions, all of which are published by Salesforce. The extension pack has the following extensions as of now which will be activated automatically as you install this pack:-
  1. Salesforce CLI Integration
  2. Apex
  3. Visualforce
  4. Aura Components
  5. Apex Interactive Debugger
  6. Apex Replay Debugger
  7. Lightning Web Components
Many more extensions can be added to this extension pack in future as it's officially maintained by Salesforce.

Connecting with your Salesforce Org

Now, as you've installed Salesforce DX CLI, VS Code and all the required extensions, you're good to connect your IDE with your Salesforce Org. To do so, use command:- Ctrl + Shift + P or go to the View -> Command Palette and you'll see the command palette. Start writing:- Create Project with Manifest and press enter on the first option as shown below:-


In the next step, you need to enter the project name as shown below:-


Next, you need to select the location where you want the project to be created. Move to the desired location in the dialog box as shown below and click on Create Project


Your project will be created and you'll see the project structure in the left panel of VS Code which will be similar to the below image.


Now, we need to authorize an org. Press:- Ctrl + Shift + P and type Authorize an Org. Here, we're going to connect with our dev org or a sandbox instead of a dev hub or scratch org. You'll see the first option as:- Authorize an Org and press enter.


Next, we need to select the environment we need to connect to:-


You can connect to a sandbox or a production environment and even specify a custom url. Here, I am going to connect with my dev org which is a production environment for me. Just select your environment and press enter.

Now, we need to enter the org alias i.e. a familiar username for your org. I personally keep the same name which I have entered as the project name. In my case it's:- SFDCStopOrg. Just enter the alias as shown in the below image and press enter.


It'll automatically open your default browser and the login page where you can login with your org credentials. After logging in, Salesforce DX CLI will ask for the required permissions as shown in the below image, click Allow.


It'll give Salesforce DX CLI the required permissions and will open your org. Switch back to VS Code and you'll see a message similar to the below image:-


As shown in the above image, you may now close the browser. Now, we've successfully authorized our org. The next step is to get all our classes, triggers and other required files in our IDE. To do so, open package.xml from the manifest subfolder present in the left pane. Right click anywhere in package.xml and click the option:- SFDX: Retrieve Source in Manifest from Org as shown in the below image:-


It'll retrieve all the metadata from your org which is specified in your package.xml file. For more information on how to create your package.xml, you can refer to the official salesforce documentation here:- https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/manifest_samples.htm

Once, you've fetched all the data, you can see it under the force-app folder in the respective subfolders as shown below:-


We can open any class, lightning component, vf page or any other metadata by simply clicking on it and it'll show in the VS Code Editor as shown in the below image:-


You can do your changes and push the required changes to your org by right clicking and selecting:- SFDX: Deploy This Source to Org option. Similarly, to fetch the updated file from org to VS Code IDE, you can select SFDX: Retrieve This Source from Org. In order to get the whole org's refresh just move on to your package.xml, right click and select SFDX: Retrieve Source in Manifest from Org option.

So, that's all for this tutorial, give it a try and let me know in the comments down below if you're able to use VS Code for your development tasks and even if you face any issue while implementing the above process.

Happy Trailblazing..!!

75 comments:

  1. Great Tutorial!! I have a question. Is there any way to get the complete package.xml file using VSCode?

    ReplyDelete
    Replies
    1. Hi Gaurav,

      There is a VSCode extension named as:- Salesforce Package.xml Generator Extension for VS Code which will help you to generate package.xml dynamically within VSCode. You can download it here:- https://marketplace.visualstudio.com/items?itemName=VignaeshRamA.sfdx-package-xml-generator

      Delete
  2. Thank you for taking time to write these instructions out. Lifesaver!

    ReplyDelete
    Replies
    1. Glad to know that it helped wiggity :-) Make sure to share it too..!!

      Delete
  3. Kill the process running on port 1717 or use a custom connected app and update OauthLocalPort in the sfdx-project.json file.
    16:25:12.188 sfdx force:auth:web:login --setalias vscodeOrg --instanceurl http://login.salesforce.com --setdefaultusername ended with exit code 1

    ReplyDelete
    Replies
    1. Hey, this error is occuring because VSCode is trying to use port 1717 however, any other process is already running on that port. You can navigate to the sfdx-project.json file and add an entry as:- "OauthLocalPort" : "1919" or any other port number that maybe free in your system. Hope that helps :-) Thanks for reaching out.

      Delete
    2. Hi! Rahul,
      is it works without using custom connected app

      Delete
  4. I have authorised my production org, and the left hand pane looks identical to yours except I don't have the manifest folder with the package.xml in, how do I perform the last step?

    ReplyDelete
    Replies
    1. Hi, please make sure you've initially created the project using Create Project with Manifest option. You should have the manifest folder with package.xml inside it at that instant itself.

      Delete
    2. Yes, I realised my mistake, thanks!
      Would you happen to know how I can bring in the Salesforce Reports? I was hoping to use this to manipulate their folder locations (bulk moves). Previous guides suggested using Eclipse IDE but that's been retired now and replaced with this as far as I understand.

      Delete
    3. You can try adding the reports to the package.xml like:-

      <types>
      <members>unfiled$public/Merchandise_in_Stock</members>
      <name>Report</name>
      </types>
      <types>
      <members>*</members>
      <name>ReportType</name>
      </types>

      Use * for all reports, otherwise specify the name. Then you can right click again and click on:- SFDX: Retrieve Source in Manifest from Org to fetch the latest data.

      Delete
    4. Thanks for this. I have tried both * and foldername$public/* as members but no reports are downloaded. foldername$public/report_name works however for a single report. Any idea how to download all or all in one folder?

      Delete
    5. I just checked the docs and got to know that you can't use the wildcard * for reports, so I think you need to specify the name of each report in the package.xml, that's the only option:- https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_report.htm

      But that's also not an issue, we have a VSCode Extension for this, have a look at the third extension in this blog:- https://www.sfdcstop.com/2019/06/top-10-vscode-extensions-for-salesforce.html

      Delete
    6. That's helpful thanks. I managed to get them all downloaded. I tried to move just a couple of them in the file structure (per the Eclipse IDE approach) and then highlight and deploy them back to the server using the menu option but I got the message:
      sf:INVALID_OPERATION: INVALID_OPERATION: testLevel of NoTestRun cannot be used in production organizations
      I popped one of the commands into the terminal adding "-l RunLocalTests" and that appears to have worked okay. Is there any way I can add this additional switch to the resulting commands from the "SFDX: Deploy Source to Org" menu option so I can highlight a number of files and deploy at once and not manually enter commands for each file?

      Delete
    7. VSCode is not mainly built for production deployments and you cannot directly deploy code to production using SFDX without running any tests. If you are using a windows system, this might help you:- https://www.sfdcstop.com/2019/06/sfdx-deploy-tool.html

      Delete
    8. Happy to run tests, that's what the command did in VSCode's own terminal and the production deployment was successful. All I'm looking to do now is augment the commands generated via the menu in the UI to include this extra switch.
      I took a quick look at this tool you linked but I don't think it will provide me anything more useful that VSCode's terminal already does for this use case.

      Delete
    9. Yup, it's the same. It's just you don't need to copy and paste commands in terminal and you can use it to deploy easily from one org to another by setting it up only once.

      Delete
  5. Hi Rahul how can i connect multiple orgs using VS code? Do i just follow the same process for another org? Please let me know. Thanks.

    ReplyDelete
    Replies
    1. Hi, I haven't seen any option to authorize multiple orgs for the same source code. So, it's the same process if you want to connect multiple orgs. Each org will be linked to it's own project.

      Delete
  6. How Can I get a specific class to download ? Basically I want to download XML for this class - presenceUserConfigs

    https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_presenceuserconfig.htm

    ReplyDelete
    Replies
    1. Hi, it's already shown in the link you shared, you can include the same in package.xml with the name:- PresenceUserConfig

      Delete
  7. Excellent! This is exactly what I was looking for.

    ReplyDelete
    Replies
    1. Thanks for the feedback Chet, make sure to share it among others too :-)

      Delete

  8. display accounts detail table format using in grid view using lightning component.
    please tell me

    ReplyDelete
    Replies
    1. Use lightning datatable for the same:- https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/example

      Delete
  9. -->display accounts in grid view using lightning.

    ReplyDelete
    Replies
    1. Use lightning datatable for the same:- https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/example

      Delete
  10. Replies
    1. You're most welcome Anne. Happy to know that you liked it :-) Make sure to share it among others too..!!

      Delete
  11. Hi Rahul,
    I accidentally found this blog which contains the information is super useful and well written with screen shots.
    Thanks for sharing

    ReplyDelete
    Replies
    1. Glad to know that you liked it buddy :-) Keep learning, and do share it in your network..!!

      Delete
  12. Great tutorial. Worked perfectly. Thanks.

    ReplyDelete
    Replies
    1. Happy to know that RJ. Make sure to share it among your team :-)

      Delete
  13. Thanks for the step by step instruction.
    able to setup and running in less than 15 minutes.
    also shared with all my team members.
    finally entire team moved from eclipse force.com editor to VS code with your help.

    ReplyDelete
    Replies
    1. Wow...!! That's amazing. I am so happy to know that it helped :-)

      Delete
  14. Thanks buddy!!

    Could you please also create one blog for Gitlab CI/CD pipeline?

    ReplyDelete
    Replies
    1. Hi Piyush, sure buddy. I actually gave a live session on the same stuff at TrailheaDX India 2018. Will try to replicate that in the form of a blog. Meanwhile you can add a message on our group:- https://t.me/joinchat/LavReljDKkknR_k8cnM-HQ so that I keep a note of the same.

      Delete
  15. i have accidentally authorized my free developer account instead of my trial head play account, how can i unauthorized the developer account ?

    ReplyDelete
    Replies
    1. You can simply re-authorize the project with your trailhead org following the same steps.

      Delete
  16. Hi Rahul,
    when i am trying to Retrieve Source from Org ,i am getting
    === Retrieved Source
    No results found.

    Thanks in advance.

    ReplyDelete
    Replies
    1. Hi,
      Is it a scratch org ? It usually occurs in scratch org when you've already retrieved source once and it doesn't track it again. Can you check which org you're connected to by executing open default org in vscode ?

      Delete
  17. am not able authorize the project, please help me

    ReplyDelete
    Replies
    1. What's the issue ? Can you post it up in our telegram group here:- https://t.me/sfdcstopdiscuss ?

      Delete
  18. not able authorize the project

    ReplyDelete
  19. Good article Rahul.

    When I try to retrieve source from org by right clicking on package.xml I get below. Any reason why?

    Starting SFDX: Retrieve Source from Org

    19:33:49.296 sfdx force:source:retrieve --manifest c:\Karthik\Salesforce\workspace\training\manifest\package.xml
    === Retrieved Source
    No results found
    19:34:09.856 sfdx force:source:retrieve --manifest c:\Karthik\Salesforce\workspace\training\manifest\package.xml ended with exit code 0

    ReplyDelete
    Replies
    1. Hi Kartik, can you please make sure you're connected to the right org ? and your package.xml specifies the data that you're trying to fetch ? You can DM me on telegram or post your query along with images in our group here:- https://t.me/sfdcstopdiscuss and I'll get back to you asap

      Delete
  20. HI Rahul,
    good day...I am a .net developer and recently moved to salesforce. I have cloned my sales force project from the git in to vscode.we are not SFDX yet.So finding it hard to get any sort of help.I am trying associate my project to a sandbox so that the chages i made would get reflected.Not sure how to do that.Kindly help

    ReplyDelete
    Replies
    1. Hi Bhavani, Welcome to Salesforce Ohana :-)

      You can connect your project to sandbox in the same way as shown above. Have you tried that ? Are you facing any issues ?

      Delete
  21. Hi Rahul I followed the above step still i am unable to authorize org : below is the error:-

    Error: spawn cmd ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    errno: 'ENOENT',
    code: 'ENOENT',
    syscall: 'spawn cmd',
    path: 'cmd',
    spawnargs:
    [ '/c',
    'start',
    '""',
    '/b',
    'https://login.salesforce.com//services/oauth2/authorize?response_type=code^&client_id=PlatformCLI^&redirect_uri=http%3A%2F%2Flocalhost%3A1717%2FOauthRedirect^&state=a2cd3379aa67^&prompt=login^&scope=refresh_token%20api%20web^&code_challenge=kOU9uW2cYTaFOwr_55RRL5OaVeP0-3oyPJY9kCvC7IQ' ] }
    Starting SFDX: Authorize an Org

    ReplyDelete
    Replies
    1. Hi Gaurav,

      It seems like your sfdx is not working fine, can you try re-installing or updating it using "sfdx update" command.

      Delete
  22. My CLI is upto date and even i Tried by re-install.. But I found the same Issue

    ReplyDelete
    Replies
    1. Hi, which issue are you talking about here ? Can you please explain ?

      Delete
  23. Hi,

    I have tried adding comments to my apex class code and then right click "SFDX: Deploy source to ORG". But I don't see the changes in my org when I go to the apex class in my org. Anything I should check?

    ReplyDelete
    Replies
    1. Hi, can you check if that command is running successfully (by checking the output tab for salesforce cli) and you're checking the org that's connected to VSCode ?

      Delete
  24. Hi Rahul, I believe we can't work at a time on multiple sandbox orgs like eclipse? If so can you describe how to work on multiple orgs in same/different workspace.

    ReplyDelete
    Replies
    1. Hi Prasad, you can change the org by clicking on the "Plug" icon that's there in VSCode footer. You can also open command palette and choose:- Set a default org option to set/change a default org which will enable you to work on multiple orgs in same project

      Delete
  25. Hi Rahual, I believe it's not possible work on multiple orgs in same workspace at a time? if not can you please describe how to work on multiple orgs at a time in same/different workspace.

    ReplyDelete
  26. hi rahul iam unable to authorize an org option is not showing vs why

    ReplyDelete
    Replies
    1. Hi, I guess the extensions are not loaded properly in VSCode, try opening the command pallete and choose "Reload Window" and find the authorize option, it should be there, also make sure you've installed Salesforce Extension Pack in vscode

      Delete
  27. Rahul...Thanks so much...Connection to a non scratch org never been explained this clearer before....thanks for your time and effort...you are definitely making the new comers life easier...cheers

    ReplyDelete
    Replies
    1. Happy to hear that it helped bhavani, thank you!

      Delete
  28. Thank you Rahul for guide with screenshots.. after going through multiple URLs for setting up VS code this really worked. easily explained steps..

    ReplyDelete
  29. Rahul, thank you for sharing your knowledge in such an easy to understand way. I'm not a developer and I had been using eclipse for 4 years before all the salesforce and mac updates destroyed that path haha. Keep up the good work. After so many attempts to decipher documentation and other tutorials, I got this up and running in half an hour thanks to you!

    ReplyDelete
    Replies
    1. That's so amazing!! Thank you for letting me know, I am glad it helped.

      Delete
  30. Hi Rahul,

    If i open my vs code next time after all the installation and connecting to salesforce.
    How to switch between different orgs and how to set my default org

    ReplyDelete
    Replies
    1. Hi Mahesh, if you open the command pallete using Ctrl + Shift + P (Command + Shift + P for mac) you'll see an option there: SFDX: Set a Default Org

      Delete
  31. Thanks for the detail info..

    ReplyDelete