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.
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.
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:-
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:-
- Salesforce CLI Integration
- Apex
- Visualforce
- Aura Components
- Apex Interactive Debugger
- Apex Replay Debugger
- 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.
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.
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:-
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..!!
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..!!
Great Tutorial!! I have a question. Is there any way to get the complete package.xml file using VSCode?
ReplyDeleteHi Gaurav,
DeleteThere 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
Thanks Rahul
Deletethanks
DeleteThank you for taking time to write these instructions out. Lifesaver!
ReplyDeleteGlad to know that it helped wiggity :-) Make sure to share it too..!!
DeleteKill the process running on port 1717 or use a custom connected app and update OauthLocalPort in the sfdx-project.json file.
ReplyDelete16:25:12.188 sfdx force:auth:web:login --setalias vscodeOrg --instanceurl http://login.salesforce.com --setdefaultusername ended with exit code 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.
DeleteHi! Rahul,
Deleteis it works without using custom connected app
Yes Ramesh, It will work.
DeleteI 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?
ReplyDeleteHi, 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.
DeleteYes, I realised my mistake, thanks!
DeleteWould 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.
You can try adding the reports to the package.xml like:-
Delete<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.
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?
DeleteI 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
DeleteBut 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
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:
Deletesf: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?
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
DeleteHappy 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.
DeleteI 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.
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.
DeleteHi 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.
ReplyDeleteHi, 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.
DeleteHow Can I get a specific class to download ? Basically I want to download XML for this class - presenceUserConfigs
ReplyDeletehttps://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_presenceuserconfig.htm
Hi, it's already shown in the link you shared, you can include the same in package.xml with the name:- PresenceUserConfig
DeleteExcellent! This is exactly what I was looking for.
ReplyDeleteThanks for the feedback Chet, make sure to share it among others too :-)
Delete
ReplyDeletedisplay accounts detail table format using in grid view using lightning component.
please tell me
Use lightning datatable for the same:- https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/example
Delete-->display accounts in grid view using lightning.
ReplyDeleteUse lightning datatable for the same:- https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/example
DeleteThank you
ReplyDeleteYou're most welcome Anne. Happy to know that you liked it :-) Make sure to share it among others too..!!
DeleteHi Rahul,
ReplyDeleteI accidentally found this blog which contains the information is super useful and well written with screen shots.
Thanks for sharing
Glad to know that you liked it buddy :-) Keep learning, and do share it in your network..!!
DeleteGreat tutorial. Worked perfectly. Thanks.
ReplyDeleteHappy to know that RJ. Make sure to share it among your team :-)
DeleteThanks for the step by step instruction.
ReplyDeleteable 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.
Wow...!! That's amazing. I am so happy to know that it helped :-)
DeleteThanks buddy!!
ReplyDeleteCould you please also create one blog for Gitlab CI/CD pipeline?
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.
Deletei have accidentally authorized my free developer account instead of my trial head play account, how can i unauthorized the developer account ?
ReplyDeleteYou can simply re-authorize the project with your trailhead org following the same steps.
DeleteHi Rahul,
ReplyDeletewhen i am trying to Retrieve Source from Org ,i am getting
=== Retrieved Source
No results found.
Thanks in advance.
Hi,
DeleteIs 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 ?
Quite Helpful,
ReplyDeleteThank you!
Thanks for reading :-)
Deleteam not able authorize the project, please help me
ReplyDeleteWhat's the issue ? Can you post it up in our telegram group here:- https://t.me/sfdcstopdiscuss ?
Deletenot able authorize the project
ReplyDeleteWhat's the issue ?
DeleteGood article Rahul.
ReplyDeleteWhen 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
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
DeleteHI Rahul,
ReplyDeletegood 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
Hi Bhavani, Welcome to Salesforce Ohana :-)
DeleteYou can connect your project to sandbox in the same way as shown above. Have you tried that ? Are you facing any issues ?
Hi Rahul I followed the above step still i am unable to authorize org : below is the error:-
ReplyDeleteError: 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
Hi Gaurav,
DeleteIt seems like your sfdx is not working fine, can you try re-installing or updating it using "sfdx update" command.
My CLI is upto date and even i Tried by re-install.. But I found the same Issue
ReplyDeleteHi, which issue are you talking about here ? Can you please explain ?
DeleteHi,
ReplyDeleteI 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?
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 ?
DeleteHi 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.
ReplyDeleteHi 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
DeleteHi 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.
ReplyDeletehi rahul iam unable to authorize an org option is not showing vs why
ReplyDeleteHi, 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
DeleteRahul...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
ReplyDeleteHappy to hear that it helped bhavani, thank you!
DeleteThank you Rahul for guide with screenshots.. after going through multiple URLs for setting up VS code this really worked. easily explained steps..
ReplyDeleteAmazing..!! Keep learning.
DeleteRahul, 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!
ReplyDeleteThat's so amazing!! Thank you for letting me know, I am glad it helped.
DeleteHi Rahul,
ReplyDeleteIf 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
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
Deletegood one
ReplyDeleteThanks for the detail info..
ReplyDelete