Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active Apr 23, 2018
Embed
What would you like to do?
if [ -f $HOME/.bashrc_local ]; then
. $HOME/.bashrc_local
fi
export SFDC_PROD_USER="user@example.com"
export SFDC_PROD_PASS="password"
export SFDC_STAGE_USER="user@example.com.stage"
export SFDC_STAGE_PASS="password"
export SFDC_QA_USER="user@example.com.qa"
export SFDC_QA_PASS="password"
export SFDC_DEV_USER="user@example.com.dev"
export SFDC_DEV_PASS="password"
source /home/pcon/.bashrc_credentials
source /home/pcon/.bashrc_sfdc_generic
alias jsforce_prod="jsforce -u \"$SFDC_PROD_USER\" -p \"$SFDC_PROD_PASS\""
alias jsforce_stage="jsforce --sandbox -u \"$SFDC_STAGE_USER\" -p \"$SFDC_STAGE_PASS\""
alias jsforce_qa="jsforce --sandbox -u \"$SFDC_QA_USER\" -p \"$SFDC_QA_PASS\""
alias jsforce_dev="jsforce --sandbox -u \"$SFDC_DEV_USER\" -p \"$SFDC_DEV_PASS\""
export SFDC_APIVER="35.0"
function getFieldList() {
OBJ_NAME=$1
setsfdcvars $2
if [ $SFDC_SANDBOX == true ]
then
jsforce --sandbox -u "$SFDC_USER" -p "$SFDC_PASS" -e "describe('$OBJ_NAME')" | jq '[.fields[].name] | join(",")' | sed 's/"//g'
else
jsforce_prod -e "describe('$OBJ_NAME')" | jq '[.fields[].name] | join(",")' | sed 's/"//g'
fi
}
function getObject() {
if [ $# -lt 2 ]
then
echo "getObject objectName Id [sandbox]"
return 1
fi
OBJ_NAME=$1
ID=$2
setsfdcvars $3
FIELDS=$(getFieldList $OBJ_NAME $3)
if [ $SFDC_SANDBOX == true ]
then
jsforce --sandbox -u "$SFDC_USER" -p "$SFDC_PASS" -e "query(\"select $FIELDS from $OBJ_NAME where Id = '$ID'\")" | jq '.records[] | del(.attributes)'
else
jsforce_prod -e "query(\"select $FIELDS from $OBJ_NAME where Id = '$ID'\")" | jq '.records[] | del(.attributes)'
fi
}
function getObjectByParent() {
if [ $# -lt 3 ]
then
echo "getObjectByParent objectName parentField parentId [sandbox]"
return 1
fi
OBJ_NAME=$1
PARENT_FIELD=$2
ID=$3
setsfdcvars $4
FIELDS=$(getFieldList $OBJ_NAME $4)
if [ $SFDC_SANDBOX == true ]
then
jsforce --sandbox -u "$SFDC_USER" -p "$SFDC_PASS" -e "query(\"select $FIELDS from $OBJ_NAME where $PARENT_FIELD = '$ID'\")" | jq '[.records[] | del(.attributes)]'
else
jsforce_prod -e "query(\"select $FIELDS from $OBJ_NAME where $PARENT_FIELD = '$ID'\")" | jq '[.records[] | del(.attributes)]'
fi
}
function echoerr() {
>&2 echo -e "$@"
}
function setsfdcvars() {
SFDC_HOST=${1^^}
if [ -z $SFDC_HOST ]
then
SFDC_HOST='PROD'
fi
if [ $SFDC_HOST == 'PROD' ]
then
SFDC_SANDBOX=false
else
SFDC_SANDBOX=true
fi
UVAR="SFDC_${SFDC_HOST}_USER"
PVAR="SFDC_${SFDC_HOST}_PASS"
EVAR="SFDC_${SFDC_HOST}_EMAIL"
export SFDC_USER=${!UVAR}
export SFDC_PASS=${!PVAR}
export SFDC_EMAIL=${!EVAR}
}
function setuservars() {
ENVNAME="${1^^}"
UVAR="STRATA_${ENVNAME}_USER"
PVAR="STRATA_${ENVNAME}_PASS"
HVAR="STRATA_${ENVNAME}_HOST"
USER="${!UVAR}"
PASS="${!PVAR}"
HOST="${!HVAR}"
}
function setformat() {
FORMAT="application/json"
if [ "$1" == "xml" ]
then
FORMAT="application/xml"
fi
}
function soap_login() {
setsfdcvars $1
LOGIN_FILE="/tmp/login.xml"
SFDC_ENDPOINT="https://login.salesforce.com"
if [ $SFDC_SANDBOX == true ]
then
SFDC_ENDPOINT="https://test.salesforce.com"
fi
curl -s -H 'Content-Type: text/xml' -H 'SOAPAction: ""' -X POST -d "<se:Envelope xmlns:se=\"http://schemas.xmlsoap.org/soap/envelope/\"><se:Header/><se:Body><login xmlns=\"urn:partner.soap.sforce.com\"><username>$SFDC_USER</username><password>$SFDC_PASS</password></login></se:Body></se:Envelope>" "$SFDC_ENDPOINT/services/Soap/u/$SFDC_APIVER" > $LOGIN_FILE
export SFDC_SESSIONID=`cat $LOGIN_FILE | xmllint --xpath "//*[name()='sessionId']/text()" -`
export SFDC_SERVERURL=`cat $LOGIN_FILE | xmllint --xpath "//*[name()='serverUrl']/text()" - | sed 's/\/Soap.*$//'`
}
function print_status_code() {
STATUS_CODE=$1
MSG=""
if [ ! -z "$2" ]
then
MSG=" - $2"
fi
case $STATUS_CODE in
200)
echoerr "[${GREEN}OK${NC}] $STATUS_CODE$MSG\n"
;;
*)
echoerr "[${RED}BAD${NC}] $STATUS_CODE$MSG\n"
;;
esac
}
function sfdc_email() {
if [ $# -lt 1 ]
then
echo "Usage: sfdc_email from_address [sandbox]"
fi
setsfdcvars $2
TMP_BODY="/tmp/TMP_$RANDOM"
DATE_F=`date`
echo "Test case $DATE_F" > $TMP_BODY
mutt -s "Test case $DATE_F" -e "my_hdr From:$1" $SFDC_EMAIL < $TMP_BODY
rm $TMP_BODY
}
function parse_headers() {
REQUEST_FILE=$1
if [ -e $REQUEST_FILE ]
then
ADDITIONAL_HEADERS=`cat $REQUEST_FILE | jq '.headers | to_entries | map("-H \"" + .key + ": " + .value + "\"")[]' | sed 's/^"//' | sed 's/"$//' | sed 's/\\\"/\"/g' | sed ':a;N;$!ba;s/\n/ /g'`
fi
}
function sfdc_curl() {
if [ $# -lt 2 ]
then
echo "Usage: sfdc_curl type \"path\" [request.json] [sandbox]"
return 1
fi
REQUEST_FILE=$3
if [ ! -f $REQUEST_FILE ]
then
echo "Unable to find \"$REQUEST_FILE\""
return 1
fi
parse_headers $REQUEST_FILE
soap_login $4
OUTFILE="/tmp/TMP_$RANDOM"
HEADERFILE="/tmp/TMP_$RANDOM"
URI=`echo "$2" | sed 's/^\///'`
CURL_TYPE=$1
MSG_HEADER="Message"
CMD="curl -s -X $CURL_TYPE $ADDITIONAL_HEADERS -D $HEADERFILE -o $OUTFILE -w \"%{http_code}\" -H \"Authorization: Bearer $SFDC_SESSIONID\" -H \"Content-Type: application/json\""
if [ "$CURL_TYPE" == "POST" ] || [ "$CURL_TYPE" == "PATCH" ]
then
DATA_FILE="/tmp/TMP_$RANDOM"
cat $REQUEST_FILE | jq '.data' > $DATA_FILE
CMD="$CMD -d @$DATA_FILE"
fi
CMD="$CMD \"$SFDC_SERVERURL/apexrest/$URI\""
STATUS_CODE=`eval $CMD`
HEADER_MSG=""
if [ -e "$HEADERFILE" ]
then
HEADER_MSG=`cat $HEADERFILE | sed -En "s/^$MSG_HEADER: (.*)/\1/p"`
rm $HEADERFILE
fi
print_status_code $STATUS_CODE "$HEADER_MSG"
if [ -e "$OUTFILE" ]
then
cat $OUTFILE
rm $OUTFILE
fi
if [ -e "$DATA_FILE" ]
then
rm $DATA_FILE
fi
}
function sfdc_soap() {
if [ $# -lt 1 ]
then
echo "Usage: sfdc_soap classname requestfile [sandbox]"
return 1
fi
soap_login $3
CLASS_NAME=$1
REQUEST_FILE=$2
if [ ! -f $REQUEST_FILE ]
then
echo "Unable to find \"$REQUEST_FILE\""
return 1
fi
TMPFILE="/tmp/TMP_$RANDOM"
OUTFILE="/tmp/TMP_$RANDOM"
HEADERFILE="/tmp/TMP_$RANDOM"
cat $REQUEST_FILE | sed "s/SESSION_ID/$SFDC_SESSIONID/g" > $TMPFILE
CMD="curl -X POST -D $HEADERFILE -o $OUTFILE -w \"%{http_code}\" -H \"Content-Type: text/xml;charset=UTF-8\" -H \"SOAPAction: \\\"\\\"\" --data $TMPFILE"
CMD="$CMD \"$SFDC_SERVERURL/Soap/class/$CLASS_NAME\""
echo $CMD
STATUS_CODE=`eval $CMD`
HEADER_MSG=""
if [ -e "$HEADERFILE" ]
then
HEADER_MSG=`cat $HEADERFILE | sed -En "s/^$MSG_HEADER: (.*)/\1/p"`
rm $HEADERFILE
fi
print_status_code $STATUS_CODE "$HEADER_MSG"
if [ -e "$OUTFILE" ]
then
xmllint --format $OUTFILE
rm $OUTFILE
fi
if [ -e "$TMPFILE" ]
then
rm $TMPFILE
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment