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:
CURRENTYR=2017 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:
#!/bin/bash CURRENT_YEAR=$(date +%Y) QUERY="SELECT Id, AccountId, CloseDate, Account.Industry FROM Opportunity WHERE StageName = 'Closed Won' AND CALENDAR_YEAR(CloseDate) = $CURRENT_YEAR" 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