SOQL from the Command Line

SOQL and the Command Line go together better than peanut butter and jelly. Here's why.
Once upon a time, I got tired of running Salesforce Reports to get miscellaneous data. I found Workbench and started using their interface to run SOQL queries. This was great for a while, but I knew there was a better way.

Pro-tip with Workbench: If you set a browser bookmark after your query runs, you can return to that bookmark and it will re-run your query!

Enter the SFDX Command Line Interface (CLI). Get ready to take your productivity into hyperdrive! 🚀🚀🚀

sfdx force:data:soql:query

If there's one command you learn, this is the one. All you have to do is pass a query string into the -q parameter and you're in business. Check it out:

-r | --resultformat

This is my 2nd favorite thing. How often do you need to export a csv from Salesforce? And how annoying is it to make throwaway Salesforce reports each time? Yeah, it's the worst.

-r to the rescue! Just add "-r csv" to the end of your command and the data will return as comma separated values. Now just redirect the output to a csv file and you've exported Salesforce data right from the command line!

If you're not too familiar with how to redirect the output of a command, here's a StackExchange link that gives a great explanation. I'm redirecting in the gif below, so check that out for a live example:

Don't like all the typing? Set an alias!

Yeah I know, "sfdx force:data:soql:query -q " is a lot to type each time. Instead of all that typing, just set an alias! I alias it to "soql" because it creates a very natural workflow for me:


Getting Started

While the Salesforce Command Line Interface (CLI) is part of SFDX, you can use it without using the other SFDX tools. To get started, head over to this link to install the CLI or check out the detailed setup instructions on Trailhead.

Once you've got it up and running, use one of the sfdx force:auth commands to connect the CLI to your org. The sfdx force:auth:web:login command is probably the easiest one to get started.

From there, start running SOQL! You now have data right at your fingertips!

Comments

  1. I just wanted to say this is an elegantly composed article as we have seen here. I got some knowledge from your article and also it is a significant article for us. Thanks for sharing an article like this.Indonesia import data

    ReplyDelete

Post a Comment

Popular posts from this blog

Joins in SOQL

Modify Salesforce Data from the Command Line