Project👨💻
Configure and integrate the recommended IDE for Salesforce development.
🔗TRAILHEAD👨💻:https://trailhead.salesforce.com/pt-BR/content/learn/projects/quickstart-vscode-salesforce
🔗My Trailhead Salesforce profile:https://trailblazer.me/id?lang=pt_BR
In this step, we will explore some of the more advanced features of Visual Studio Code, for example, how to use the integrated terminal and our newly installed Salesforce Extension Pack.
As with any good development tool, there is more than one way to do things with Visual Studio Code. The two main ways to interact with the Salesforce CLI are through the integrated terminal or the quick opening window.
To display the quick opening window, press Command + P on the Mac or Ctrl + P on Windows. If you type ?
, you can see the help menu. In this module, we will use the quick opening window in the command palette mode, which allows us to show and execute commands.
- Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to open the command palette.
- Make sure the new prompt starts with
>
- Type SFDX: Create Project and press Enter to select the default model.
- Type the name of the project
VSCodeQuickstart
and press Enter . - Select your workspace as the place to create the project to make it easier to find later.
- Wait for the new Visual Studio Code window to open. You should see an indication that the extension is preparing your project before filling out the file explorer.
- Press ** Command + P ** on Mac or Ctrl + P on Windows to make the search palette appear. This shifts the focus to searching for files.
- Type
project-scratch-def.json
in the field. - Click on the result to open the file.
- On the left side of Visual Studio Code, click the Find and Replace menu.
- Search
orgName
. - In the first result found in project-scratch-def.json.
- Change the value of orgName (after: and enter “”) to
Learning VS Code
. - Save the file by pressing Command + S on the Mac, Ctrl + S on Windows.
- Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to open the command palette.
- Type
SFDX: Authorize an Org
. - To accept the default login URL, press Enter.
- Enter the alias
VSCodePlayground
. - Note that your default browser opens a new Salesforce login window. Log in to your Playground using the Playground username and password retrieved in the last step.
- When access to the connected application is requested, click to allow.
- Close the browser window.
The command line terminal window returns a success message when the transaction is complete.
-
Click the Explorer icon in Visual Studio Code to expand the force-app folder.
-
In the VSCODEQUICKSTART directory, click force-app to show the folder tree. In the force-app / main / default directory, you can find metadata included in the project, such as applications, aura, classes and more.
-
Press Command + Shift + P on Mac or **Ctrl + Shift + **P on Windows to open the command palette.
-
Type
SFDX: Create Apex Class
. -
Enter the name
AccountController
. -
If VS Code asks, select force-app / main / default / classes as the directory to which you want to add
AccountController.cls
. -
In the newly opened AccountController.cls file, replace the standard code with the following:
-
public with sharing class AccountController { public static List<Account> getAllActiveAccounts() { return [SELECT Id,Name,Active__c FROM Account WHERE Active__c = 'Yes']; } }
Copy
-
Save the file.
Our new Apex class has a SOQL query in it, but we want to make sure that it works as intended before deploying it to our organization. We use the command palette to run the query against our organization.
- In line 3 of the code, highlight the query
SELECT Id,Name,Active__c FROM Account WHERE Active__c = 'Yes’
- Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to open the command palette.
- Search
SFDX:Execute SOQL Query with Currently Selected Text
. - Press Enter .
- Select REST API and press Enter .
- On the Output tab of the integrated terminal window, view the results of your query. The window should contain a summary that says: SFDX: Run SOQL Query ... ended with exit code 0. This means that it was executed successfully.
The last step is to deploy your code to your Playground using Visual Studio Code.