Use Visual Studio Code for Salesforce development

Project👨‍💻

Quickstart: Visual Studio Code for Salesforce development

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.

Terminal vs. command palette

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.

The display of global commands in the quick opening window with?  in the field.

Create a project

  1. Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to open the command palette.
  2. Make sure the new prompt starts with >
  3. Type SFDX: Create Project and press Enter to select the default model.
  4. Type the name of the project VSCodeQuickstartand press Enter .
  5. Select your workspace as the place to create the project to make it easier to find later.
  6. 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.

Extension notice: Running SFDX: Create Project.

Search your files

  1. Press ** Command + P ** on Mac or Ctrl + P on Windows to make the search palette appear. This shifts the focus to searching for files.
  2. Type project-scratch-def.jsonin the field.
  3. Click on the result to open the file.
  4. On the left side of Visual Studio Code, click the Find and Replace menu. forget
  5. Search orgName.
  6. In the first result found in project-scratch-def.json.
  7. Change the value of orgName (after: and enter “”) to Learning VS Code.
  8. Save the file by pressing Command + S on the Mac, Ctrl + S on Windows.

The project-scratch-def.json file with the new name of the organization.

Authenticate to your Playground

  1. Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to open the command palette.
  2. Type SFDX: Authorize an Org.
  3. To accept the default login URL, press Enter.
  4. Enter the alias VSCodePlayground.
  5. 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.
  6. When access to the connected application is requested, click to allow. The Allow access to a global connected application page
  7. Close the browser window.

The command line terminal window returns a success message when the transaction is complete.

USER successfully authorized with organization ID / Now you can close the browser.

Create an Apex class

  1. Click the Explorer icon explorer iconin Visual Studio Code to expand the force-app folder.

  2. 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. The expanded folder tree so that you can see the class folder.

  3. Press Command + Shift + P on Mac or **Ctrl + Shift + **P on Windows to open the command palette.

  4. Type SFDX: Create Apex Class.

  5. Enter the name AccountController.

  6. If VS Code asks, select force-app / main / default / classes as the directory to which you want to add AccountController.cls.

  7. In the newly opened AccountController.cls file, replace the standard code with the following:

  8. public with sharing class AccountController {
      public static List<Account> getAllActiveAccounts() {
        return [SELECT Id,Name,Active__c FROM Account WHERE Active__c = 'Yes'];
      }
    }
    

    Copy

  9. Save the file.

Query

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.

  1. In line 3 of the code, highlight the query SELECT Id,Name,Active__c FROM Account WHERE Active__c = 'Yes’
  2. Press Command + Shift + P on Mac or Ctrl + Shift + P on Windows to open the command palette.
  3. Search SFDX:Execute SOQL Query with Currently Selected Text.
  4. Press Enter .
  5. Select REST API and press Enter .
  6. 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 Output tab showing the 10 records received from your Trailhead Playground.

Deploy

The last step is to deploy your code to your Playground using Visual Studio Code.

  1. Right-click the classes folder . With the classes folder right-clicked, SFDX: Deploy Source to Org is selected in the list of options.
  2. Click SFDX: Deploy Source to Org .
  3. On the Output tab of the integrated terminal, view the results of your deployment. You should also have received a warning that says: SFDX: Deploy Source to Org ... ended with exit code 0. This means that it ran successfully.

The Output tab showing results with exit code 0 as successful.

Salesforce Developer: