Skip to content

Instantly share code, notes, and snippets.

@gbutt
Last active April 5, 2019 22:43
Show Gist options
  • Save gbutt/8e09590755c89018d4a763b8aba2a24c to your computer and use it in GitHub Desktop.
Save gbutt/8e09590755c89018d4a763b8aba2a24c to your computer and use it in GitHub Desktop.
SFDX convert SOQL into CSV
#!/bin/sh -e
function printHelp() {
echo "Incorrect usage. Example:\n\t./soql2csv.sh \"SELECT Id, Name FROM Account LIMIT 10\" myorg > accounts.csv";
}
if [ ! -e "`which jq`" ]; then
echo "You need to install jq to use this script."
if [[ "`uname`" == "Darwin" && -e "`which brew`" ]]; then
echo "Try running 'brew install jq'"
fi
exit 1
fi
if [ "$#" -lt 1 ]; then
printHelp
exit 1
fi
QUERY=$1
if [ -z "$QUERY" ]; then
printHelp
exit 1
fi
ORG=$2
if [ -z "$ORG" ]; then
ORG=`sfdx force:config:list --json | jq -r '.result[] | select(.key == "defaultdevhubusername") | .value'`
fi
# execute SOQL and convert json result into a CSV.
sfdx force:data:soql:query -u $ORG --query "$QUERY" --json | \
jq -r '.result.records | del(.[].attributes) | (.[0] | to_entries | map(.key)), (.[] | [.[]]) | @csv '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment