Skip to content

Instantly share code, notes, and snippets.

@rtanikella
Created March 15, 2023 03:03
Show Gist options
  • Save rtanikella/e9d15faff367b5674e64d7c105f43ada to your computer and use it in GitHub Desktop.
Save rtanikella/e9d15faff367b5674e64d7c105f43ada to your computer and use it in GitHub Desktop.
Retrieves the schema description of a Salesforce sobject and emits a SOQL query listing only the fields that are 'createable=true'.
#!zsh
if [ ! -n "$2" ]
then
help_text=$( cat <<ENDxxx
$0 allows you to specify a Salesforce object defined within
an org and returns a SOQL query that returns only the createable
fields of that object. Use this query to quickly extract a CSV
of the fields of that object that you can insert or upsert to
an org without having to determine which fields will fail because
they cannot be inserted or upserted (for example, formula fields or
automatically numbered name fields.)
Usage: $0 <Salesforce object> <Salesforce org alias>
where
<Salesforce object> is the API name of a Salesforce Custom Object. This is
used to call "sfdx force:schema:describe" as the value for the
'--sobjecttype' parameter
<Salesforce org alias> is the alias of the org in which the <Salesforce object> is
defined. This is used to call "sfdx force:schema:describe" as the value
for the '--target-org' parameter (formerly the '--targetusername'
parameter.) It is assumed that this org has already been authorized
with sfdx:
sfdx auth:web:login --instanceurl="<url of target org>"
--setalias="<Salesforce org alias>"
$0 requires sfdx and jq commands to be in your PATH.
ENDxxx
)
echo "$help_text"
exit
else
CUSTOMOBJ=$1
ORG=$2
fi
sfdx force:schema:sobject:describe --sobjecttype $CUSTOMOBJ --target-org $ORG | jq --arg customObject $CUSTOMOBJ '[ .fields[] | if .createable then .name else empty end ] | join(", ") | "SELECT " + . + " FROM " + $customObject'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment