Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active May 23, 2016 15:09
Show Gist options
  • Select an option

  • Save pcon/54e8b0d7518d8fe42c79 to your computer and use it in GitHub Desktop.

Select an option

Save pcon/54e8b0d7518d8fe42c79 to your computer and use it in GitHub Desktop.

Revisions

  1. pcon revised this gist May 15, 2015. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion .bashrc_sfdc.sh
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@ export SFDC_APIVER="33.0"

    export SFDC_PROD_USER="[email protected]"
    export SFDC_PROD_PASS="1234password"
    export SFDC_PROD_SAND=false

    export SFDC_DEV_USER="[email protected]"
    export SFDC_DEV_PASS="1234password"
    @@ -12,11 +13,12 @@ export SFDC_QA_PASS="1234password"

    export SFDC_PERSONAL_USER="[email protected]"
    export SFDC_PERSONAL_PASS="1234password"
    export SFDC_PERSONAL_SAND=false

    alias jsforce_prod="jsforce -u \"$SFDC_PROD_USER\" -p \"$SFDC_PROD_PASS\""
    alias jsforce_dev="jsforce --sandbox -u \"$SFDC_DEV_USER\" -p \"$SFDC_DEV_PASS\""
    alias jsforce_qa="jsforce --sandbox -u \"$SFDC_QA_USER\" -p \"$SFDC_QA_PASS\""
    alias jsforce_personal="jsforce --sandbox -u \"$SFDC_PERSONAL_USER\" -p \"$SFDC_PERSONAL_PASS\""
    alias jsforce_personal="jsforce -u \"$SFDC_PERSONAL_USER\" -p \"$SFDC_PERSONAL_PASS\""
    alias jsforce_sandbox="jsforce --sandbox -u \"$SFDC_USER\" -p \"$SFDC_PASS\""

    function echoerr() {
    @@ -40,8 +42,14 @@ function setsfdcvars() {

    UVAR="SFDC_${SFDC_HOST}_USER"
    PVAR="SFDC_${SFDC_HOST}_PASS"
    SVAR="SFDC_${SFDC_HOST}_SAND"
    export SFDC_USER=${!UVAR}
    export SFDC_PASS=${!PVAR}

    if [ ".${!SVAR}" != "." ]
    then
    SFDC_SANDBOX=${!SVAR}
    fi
    }

    function soap_login() {
  2. pcon revised this gist May 15, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions .bashrc_sfdc.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # Name this .bashrc_sfdc
    export SFDC_APIVER="33.0"

    export SFDC_PROD_USER="[email protected]"
    export SFDC_PROD_PASS="1234password"

  3. pcon revised this gist Apr 29, 2015. 3 changed files with 5 additions and 5 deletions.
    6 changes: 3 additions & 3 deletions .bashrc
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #Add to .bashrc
    if [ -f $HOME/.bashrc_local ]
    #Add to .bashrc_sfdc
    if [ -f $HOME/.bashrc_sfdc ]
    then
    . $HOME/.bashrc_local
    . $HOME/.bashrc_sfdc
    fi
    2 changes: 1 addition & 1 deletion .bashrc_local.sh → .bashrc_sfdc.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #.bashrc_local
    # Name this .bashrc_sfdc
    export SFDC_PROD_USER="[email protected]"
    export SFDC_PROD_PASS="1234password"

    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ Install the following utilities
    * Make sure you install it with `npm -g install jsforce`

    #Installation
    1. Copy the contents of `.bashrc_local` to your `$HOME` directory
    1. Copy the contents of `.bashrc_sfdc` to your `$HOME` directory
    2. Add the snippet to your `.bashrc` to include it

    _NOTE: I put this in a separate file so that I can keep it out of source control and keep it on my work machine. There is no technical reason to separate them so you can put it all in one file if desired._
  4. pcon revised this gist Apr 29, 2015. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -32,12 +32,21 @@ This method calls APEX REST methods via cURL.
    global with sharing class REST_MyClass_Case {
    @HttpGet
    global static Case getCase() {
    Case c = null;
    String caseNumber = RestContext.request.params.get('caseNumber');
    return [
    List<Case> cases = [
    select Subject
    from Case
    where CaseNumber = :caseNumber
    ];
    if (cases.size() != 1) {
    RestContext.response.statusCode = 404;
    RestContext.response.addHeader('Message', 'Unable to find case by number');
    } else {
    c = cases.get(0);
    }
    }
    }
    ```
  5. pcon revised this gist Apr 29, 2015. 1 changed file with 24 additions and 7 deletions.
    31 changes: 24 additions & 7 deletions .bashrc_local.sh
    Original file line number Diff line number Diff line change
    @@ -61,13 +61,19 @@ function soap_login() {

    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\n"
    200)
    echoerr "[${GREEN}OK${NC}] $STATUS_CODE$MSG\n"
    ;;
    *)
    echoerr "[${RED}BAD${NC}] $STATUS_CODE\n"
    *)
    echoerr "[${RED}BAD${NC}] $STATUS_CODE$MSG\n"
    ;;
    esac
    }
    @@ -82,14 +88,25 @@ function sfdc_curl() {
    soap_login $2

    OUTFILE="TMP_$RANDOM"
    HEADERFILE="TMP_$RANDOM"
    URI=`echo "$1" | sed 's/^\///'`

    STATUS_CODE=`curl -s -o $OUTFILE -w "%{http_code}" -H "Authorization: Bearer $SFDC_SESSIONID" -H "Content-Type: application/json" "$SFDC_SERVERURL/apexrest/$URI"`
    MSG_HEADER="Message"

    STATUS_CODE=`curl -s -D $HEADERFILE -o $OUTFILE -w "%{http_code}" -H "Authorization: Bearer $SFDC_SESSIONID" -H "Content-Type: application/json" "$SFDC_SERVERURL/apexrest/$URI"`

    HEADER_MSG=""

    print_status_code $STATUS_CODE
    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
    then
    cat $OUTFILE
    rm $OUTFILE
    fi
  6. pcon revised this gist Apr 28, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Install the following utilities

    * [jq](http://stedolan.github.io/jq/) - A super handy json parser
    * [xmllint](http://xmlsoft.org/xmllint.html) - An xml linter and parser
    * [jsforce](https://jsforce.github.io/) - A great tool for working with Salesforceo on the commandline
    * [jsforce](https://jsforce.github.io/) - A great tool for working with Salesforce on the commandline
    * Make sure you install it with `npm -g install jsforce`

    #Installation
  7. pcon revised this gist Apr 28, 2015. 1 changed file with 27 additions and 1 deletion.
    28 changes: 27 additions & 1 deletion .bashrc_local.sh
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,10 @@ alias jsforce_qa="jsforce --sandbox -u \"$SFDC_QA_USER\" -p \"$SFDC_QA_PASS\""
    alias jsforce_personal="jsforce --sandbox -u \"$SFDC_PERSONAL_USER\" -p \"$SFDC_PERSONAL_PASS\""
    alias jsforce_sandbox="jsforce --sandbox -u \"$SFDC_USER\" -p \"$SFDC_PASS\""

    function echoerr() {
    >&2 echo -e "$@"
    }

    function setsfdcvars() {
    SFDC_HOST=${1^^}

    @@ -55,6 +59,19 @@ function soap_login() {
    rm $LOGIN_FILE
    }

    function print_status_code() {
    STATUS_CODE=$1

    case $STATUS_CODE in
    200)
    echoerr "[${GREEN}OK${NC}] $STATUS_CODE\n"
    ;;
    *)
    echoerr "[${RED}BAD${NC}] $STATUS_CODE\n"
    ;;
    esac
    }

    function sfdc_curl() {
    if [ $# -lt 1 ]
    then
    @@ -64,9 +81,18 @@ function sfdc_curl() {

    soap_login $2

    OUTFILE="TMP_$RANDOM"
    URI=`echo "$1" | sed 's/^\///'`

    curl -s -H "Authorization: Bearer $SFDC_SESSIONID" -H "Content-Type: application/json" "$SFDC_SERVERURL/apexrest/$URI"
    STATUS_CODE=`curl -s -o $OUTFILE -w "%{http_code}" -H "Authorization: Bearer $SFDC_SESSIONID" -H "Content-Type: application/json" "$SFDC_SERVERURL/apexrest/$URI"`

    print_status_code $STATUS_CODE

    if [ -e $OUTFILE ]
    then
    cat $OUTFILE
    rm $OUTFILE
    fi
    }

    function getFieldList() {
  8. pcon revised this gist Apr 28, 2015. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions .bashrc
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #Add to .bashrc
    if [ -f $HOME/.bashrc_local ]
    then
    . $HOME/.bashrc_local
    1 change: 1 addition & 0 deletions .bashrc_local → .bashrc_local.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #.bashrc_local
    export SFDC_PROD_USER="[email protected]"
    export SFDC_PROD_PASS="1234password"

  9. pcon created this gist Apr 28, 2015.
    4 changes: 4 additions & 0 deletions .bashrc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    if [ -f $HOME/.bashrc_local ]
    then
    . $HOME/.bashrc_local
    fi
    122 changes: 122 additions & 0 deletions .bashrc_local
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    export SFDC_PROD_USER="[email protected]"
    export SFDC_PROD_PASS="1234password"

    export SFDC_DEV_USER="[email protected]"
    export SFDC_DEV_PASS="1234password"

    export SFDC_QA_USER="[email protected]"
    export SFDC_QA_PASS="1234password"

    export SFDC_PERSONAL_USER="[email protected]"
    export SFDC_PERSONAL_PASS="1234password"

    alias jsforce_prod="jsforce -u \"$SFDC_PROD_USER\" -p \"$SFDC_PROD_PASS\""
    alias jsforce_dev="jsforce --sandbox -u \"$SFDC_DEV_USER\" -p \"$SFDC_DEV_PASS\""
    alias jsforce_qa="jsforce --sandbox -u \"$SFDC_QA_USER\" -p \"$SFDC_QA_PASS\""
    alias jsforce_personal="jsforce --sandbox -u \"$SFDC_PERSONAL_USER\" -p \"$SFDC_PERSONAL_PASS\""
    alias jsforce_sandbox="jsforce --sandbox -u \"$SFDC_USER\" -p \"$SFDC_PASS\""

    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"
    export SFDC_USER=${!UVAR}
    export SFDC_PASS=${!PVAR}
    }

    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.*$//'`
    rm $LOGIN_FILE
    }

    function sfdc_curl() {
    if [ $# -lt 1 ]
    then
    echo "Usage: sfdc_curl \"path\" [sandbox]"
    return 1
    fi

    soap_login $2

    URI=`echo "$1" | sed 's/^\///'`

    curl -s -H "Authorization: Bearer $SFDC_SESSIONID" -H "Content-Type: application/json" "$SFDC_SERVERURL/apexrest/$URI"
    }

    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 "Usage: 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 "Usage: 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
    }
    73 changes: 73 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    #About
    This is part of my customized `.bashrc` that I use daily for help with Salesforce work

    #Prerequisites
    Install the following utilities

    * [jq](http://stedolan.github.io/jq/) - A super handy json parser
    * [xmllint](http://xmlsoft.org/xmllint.html) - An xml linter and parser
    * [jsforce](https://jsforce.github.io/) - A great tool for working with Salesforceo on the commandline
    * Make sure you install it with `npm -g install jsforce`

    #Installation
    1. Copy the contents of `.bashrc_local` to your `$HOME` directory
    2. Add the snippet to your `.bashrc` to include it

    _NOTE: I put this in a separate file so that I can keep it out of source control and keep it on my work machine. There is no technical reason to separate them so you can put it all in one file if desired._

    #Configuration
    * Update the `export` lines to contain your usernames and passwords
    * Add new environments by following the pattern of `SFDC_{NAME}_USER` and `SFDC_{NAME}_PASS`
    * Add new jsforce aliases for each environment easier use

    #Usage
    ## soap_login
    This method logs in via the Salesforce SOAP endpoint and set the `SFDC_SESSIONID` and `SFDC_SERVERURL` variables for later use
    ## sfdc_curl
    This method calls APEX REST methods via cURL.

    *Apex Definition*
    ```apex
    @RestResource(urlMapping='/myClass/case/*')
    global with sharing class REST_MyClass_Case {
    @HttpGet
    global static Case getCase() {
    String caseNumber = RestContext.request.params.get('caseNumber');
    return [
    select Subject
    from Case
    where CaseNumber = :caseNumber
    ];
    }
    }
    ```

    *Get case in production*
    ```bash
    sfdc_curl "/myClass/case/?caseNumber=012345"
    ```

    *Get case in dev*
    ```bash
    sfdc_curl "/myClass/case/?caseNumber=012345" dev
    ```
    ##getObject
    *Get case in production*
    ```bash
    getObject Case 50011000003bJSJAA2
    ```

    *Get case in dev*
    ```bash
    getObject Case 50011000003bJSJAA2 dev
    ```
    ##getObjectByParent
    *Get comments for case in production*
    ```bash
    getObjectByParent CaseComment Parent 50011000003bJSJAA2
    ```

    *Get comments for case in dev*
    ```bash
    getObjectByParent CaseComment Parent 50011000003bJSJAA2 dev
    ```