We can use SOQL in developer console or use other third-party apps to run SOQL queries but would it not be nice to just do it right in VS Code? Read on to see how to do just that.

We will need following;

  • Salesforce CLI (sfdx)
  • qforce (npm package)
  • Rainbow CSV (VS Code extension)
  • Save and Run (VS Code extension)

To install qforce, run the following command while in the root folder of your sfdx project.

npm install -g qforce

If you don’t have already installed node/npm go here for instructions. You should also have already installed the Salesforce CLI.

Once qforce is installed, execute following to initiate qforce configuration

qforce dev:config --init

You will see that a new folder “.qforce” is added that looks like as follows;

.qforce
├── exe.cls
├── query.soql
└── settings.json

Set a default username to use with various qforce commands as follows (Replace “dev” with your user name)

qforce dev:config --targetusername dev

Open the “query.soql” file. You will see following one line in it. At this point we can run following to run the query specified in “query.soql” file.

qforce dx:query 

You can find results in “.qforce/query.csv” file. Here is output for sample query for my dev org

"Id","Name"
"0011U00000F2uX8QAJ","Sample Bulk API Account"

With qforce, we can run queries like

SELECT * FROM Account WHERE ID = '0011U00000F2uX8QAJ'

To further streamline, we can use “Save and Run” extension to run the above query command every time “.qforce/query.soql” is updated. Here is part of my “.vscode/settings.json” file

{
    "saveAndRun": {
        "commands": [
            {
                "match": "query\\.soql$",
                "cmd": "qforce dx:query"
            }
        ]
    },
    ...
}

Now whenever we update the “.qforce/query.soql” file, the query will run automatically and we can see the results in “.qforce/query.csv”

If we want to run the same query against a different org, we can either update “.qforce/settings.json” file using command noted above or we can specify it as first line on the “qforce/query.soql” file itself. For example

// otherUserName
SELECT * 
FROM Account
WHERE BillingCity = 'Boston'

If we want to get all fields for a specific record id, we can simply put the id in our query file. Here is an example that will retrieve all fields for an account;

// myDevOrg
0011U00000F2uX8QAJ

For this to work, you should have already run following command. This command will get the data model from your org and build an index for sObject prefixes.

qforce describe --all -u myDevOrg

Hope this helps you and happy coding…