[SalesForce] SFDX : SOQL Query/Output

I am currently learning Salesforce DX/CLI to pull in data from my org.

I use the following syntax to extract Accounts data.

sfdx force:data:soql:query  -u vpy.lightninglearn@gmail.com --query "SELECT ID, NAME FROM Account"

Questions

a) Is it possible to source the query from a file instead of in the command (as many times the SOQL query may span across several lines) ?

b) Is it possible to store the output into a file instead of just displaying the results in the Command Prompt (as seen below) ?

enter image description here

Best Answer

Use batch script to get SOQL from file or write to file.

Fetch SOQL from File

Read SOQL from file into a variable and use the variable in the sfdx command. Here's a simple bat file example (there are limitations to set).

SET /p soql=<my_soql.txt
echo %soql%

sfdx force:data:soql:query -q  "%soql%" -r=csv -u='user@gmail.com' > c:\temp\result1.csv

Print output

Pipe the output to print data to a file. For e.g. the below command will output CSV to the file.

sfdx force:data:soql:query -q "SELECT Id, Name, Account.Name FROM Contact LIMIT 10" -r=csv -u='user@gmail.com' > c:\temp\result.csv
Related Topic