Skip to content
LinkedIn

Useful VS Code SFDX Commands

If you're a Salesforce developer, you probably know about SFDX commands. They're super helpful and are easily available in VS Code. Let me tell you about a few basic ones that I use a lot.

  1. Using Command Palette for SFDX commands
  2. Create query in SOQL Builder
  3. Execute SOQL Query with Currently Selected Text
  4. Execute Anonymous Apex with Currently Selected Text
  5. Get Apex Debug Logs
  6. Run Apex Tests

SFDX Command Palette

To see all the commands, press Command (or Ctrl) + Shift + P to open the command palette and type sfdx. You'll get a list of all the SFDX commands available.

SFDX Command Palette

Create query in SOQL Builder

You can build queries using SOQL Builder. It helps you add filter and order by functionalities.
You can see how your query will look before running it. Then, click Run Query to get your records.

SOQL Query Builder

Execute SOQL Query with Currently Selected Text

You don't need to go to the Developer Console to run queries anymore.
Use the Execute SOQL Query with Currently Selected Text command instead.

Below is an example of how you can use it.

  1. Select your query you want to execute, for example, in the class below, highlight the code and open command palette using Command (or Ctrl) + Shift + P

    SOQL Query Example

  2. Search for the command Execute SOQL Query with Currently Selected Text and click on it

  3. Click on REST API option

REST API Option

Here’s how the output looks like :

REST API Option

Execute Anonymous Apex with Currently Selected Text

Earlier the only way to execute code was to use the option of Execute Anonymous in Developer Console. With SFDX, you can now use the command Execute Anonymous Apex with Currently Selected Text to execute any code that you want in VS Code.

  1. Select the code that you want to execute, for example, in the class below, highlight the code and open command palette using Command (or Ctrl) + Shift + P

    Execute Anonymous

  2. Search for the command Execute Anonymous Apex with Currently Selected Text and click on it

Here’s the output and you can notice that it also shows you which part of the code you executed:

REST API Option

Get Apex Debug Logs

If you want to see what's happening in your code, use Get Apex Debug Logs. Make sure to turn on the flag in Setup first. It's just like looking at logs in Salesforce, but easier.

Search for the command in the command palette and click on the log that you want to see from the list.

Apex Debug Logs

Here's how the debug log appears in VS Code :

Apex Debug Log Example

Run Apex Tests

With Run Apex Tests, you can test all your code or just specific parts. Click Run All Tests or Run Test to see if everything's working fine.

Apex Run Tests

This is how it looks like after running the tests :

Apex Run Test Results

If you want to see the test coverage, then you need to edit your workspace settings. Go to File -> Settings, type in retrieve in the Workspace tab and check the option Retrieve-test-code-coverage

Enable code coverage

Now you can see your test coverage easily. You can also see the org wide coverage and the class coverage as well.

Test result with coverage

These simple commands can make your life as a Salesforce developer a whole lot easier. Give them a try and see for yourself!