Being a champion of “No Code”, Salesforce had long ignored developers and developer experience. But that all changed with Salesforce DX. A big part of Salesforce DX is Salesforce CLI (Command Line Interface). qforce not only enhances Salesforce CLI but it also adds to that functionality what makes command line more accessible to a broader audience. In this article we will see how to get started with qforce.

First, you should have already installed/configured the following;

  • Salesforce CLI (sfdx)
  • Node Package Manager (npm)
  • VS Code with salesforce extension package

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. Check this for VS Code extension

Once qforce is installed, execute following to initiate 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

Make sure “.qforce” folder is at the same level as “.sfdx” and/or “.vscode”. If working in Vs Code, run the following command to initiate code snippets for qforce

qforce dev:snippet --init

This will create a qforce.code-snippets file under .vscode directory.

Making queries

Open the query.soql file and type your query. Remember to save the file and then run following command to execute the query and get the results back

qforce dx:query -u myUserName

By default, results are save at “.qforce/query.csv”. We recommend to have “Rainbow CSV” extension installed as it makes reviewing results easy. Another good extension to have is “Save and Run”. You can configure it to execute above command every time you save the query.soql file.

You can also specify the username in the query.soql file instead of having to provide “-u myUserName” parameter on the command line. Assume we have following in our query.soql file;

// myUserName
SELECT Id, BillingCity
FROM Account
LIMIT 10

Now we can simply run following “qforce dx:query” without user name parameter. This can be very handy when combined with “Save and Run”.

If you are in a sfdx project and have executed “Refresh SObject Definitions”, you will get type-ahead recommendations as you type field/object names.

VS Code Type ahead

qforce gives you the capability to run queries like “SELECT * FROM Account”. You will need to run following command just once and then you can use “*” in your queries.

qforce dx:describe --all -u myUserName

Also, you can save your favorite queries as snippets by using following command;

qforce dev:snippets -q -a myFavoriteQuery

This will save the contents of your query.soql file as a snippet which you can re-use as needed.

Query Snippet Use

You can also check this for more on doing queries.

Anonymous apex execute

Running anonymous script is easy with qforce. Simply type (or copy) your script to “.qforce/exe.cls” file and run following command;

qforce dx:exe -u myUserName

As with queries, you can also save your favorite anonymous scripts as snippets by using following command;

qforce dev:snippets -e -a myFavoriteApex

This will save the contents of your exe.cls file as a snippet which you can re-use as needed.

Moving data around

qforce gives you a handy way to move data around. All you need is a little javascript to define what you need to move and if any transformations are needed and execute following

qforce dev:migrate -f path/to/migration/plan.js

Here is a sample plan to create new opportunities for all accounts where billing city is Boston.

let plan = {
    bulkStatusInterval: 30000,
    bulkStatusRetries: 18,
    source: 'dev',
    destination: 'dev',
    steps: [
        {
            name: 'accountList',
            referenceOnly: true,
            query: `SELECT Id, Name FROM Account WHERE BillingCity = 'Boston'`
        },
        {
            name: 'Opportunity',
            sobjecttype: 'Opportunity',
            externalid: 'Id',
            references: ['accountList'],
            generateData: function generateData() {
                let data = [];
                let accounts = this.accountList;
                for (let account of accounts) {
                    data.push({
                        Name: account.Name + ' Boston Campaign',
                        AccountId: account.Id,
                        CloseDate: '2020-05-15',
                        StageName: 'Prospecting'
                    });
                }
                
                return data;
            }
        }
    ]
}
module.exports = plan;

Building a migration plan is simple and easy thanks to VS Code built-in code-snippet functionality combined with qforce snippet command. Here is a quick demo.

Making plans

Also, check this post for more about migrate command.

You can download a base sfdx project with qforce configured here. Assuming you have installed sfdx and qforce, this project will get you going in no time.

Installation Issues

If you face any issues installing or using qforce, contact us by submitting this form.

If you are using mac and see “EACCESS: permission denied” error while installing qforce as highlighted in the screenshot below:

EACCESS Error

You can try again by running following command.

sudo npm install -g qforce

Help us improve qforce

There is a lot we can do with qforce. Help us improve qforce by completing this short survey. Not only you will be helping us, you may also end up winning a $25 amazon gift card. Winner (or winners) will be announced at the end of month, every month.

Happy coding and may the qforce be with you…