When using sfdx force:data:tree export with an in-line query, you can bind variables by creating them in the cli before running the export command, like so:

sfdx force:data:tree:export -q "SELECT Id FROM Opportunity WHERE CURRENT_YEAR(CloseDate) >= $CURRENTYR" -d queries/InvoiceAndCMRecords/ -u DevHub

Is there a way to work around the inability to bind variables in the query if you supply it via a path to the file where the query is stored?

So far, I've come up with a workaround where I embed the calculation for the variable I'd normally bind and the query as variables in a bash script, so the query string is dynamically calculated before being utilized by the sfdx force:data:tree:export command at the end of the script, but I'm still wondering if there's a better way to achieve this...

The bash script:

CURRENT_YEAR=$(date +%Y)

QUERY="SELECT  Id, AccountId, CloseDate, Account.Industry
FROM Opportunity
WHERE StageName = 'Closed Won'

echo "Running command: sfdx force:data:tree:export. Pulling from DevHub 
org. Records and Plan output to : InvoicesAndCMs/"

sfdx force:data:tree:export -q "$(echo $QUERY)" -d InvoicesAndCMs/ -p --json -u DevHub
  • What specifically are you trying to do that isn't working? Sorry, it isn't clear to me.
    – Ray Dehler
    Commented Oct 31, 2017 at 18:10
  • Use bound variables in my SOQL queries when using SFDX Data Export commands without having to use the hacky solution I came up with, which is embedding them in bash scripts.
    – KB145
    Commented Oct 31, 2017 at 23:31
  • If you do what you hinted as part of a script that should work fine, no? In the context of a single command directly in terminal there would be nowhere to get the value from sometimes
    – alesremta
    Commented Mar 9, 2020 at 20:47


You must log in to answer this question.

Browse other questions tagged .