Skip to content

Instantly share code, notes, and snippets.

👨‍💻
Coding

Doug Ayers douglascayers

👨‍💻
Coding
Block or report user

Report or block douglascayers

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@douglascayers
douglascayers / JWT.cls
Last active Sep 29, 2019
Sign a JWT token with only a private key
View JWT.cls
/**
* Inspired by the JWT repo by Salesforce Identity
* https://github.com/salesforceidentity/jwt/
*
* Inspired by the JWT repo by Auth0
* https://github.com/auth0/java-jwt
*
* Learn more about JWT at https://jwt.io
*/
public inherited sharing class JWT {
@douglascayers
douglascayers / create-package-version.sh
Last active Oct 31, 2019
Bash script to create a new managed package version from source control and no maintenance of sfdx-project.json
View create-package-version.sh
#!/usr/bin/env bash
# =========================================================================== #
# USAGE
# -----
# create-package-version.sh [branchName]
#
# The $1 argument (optional) indicates the git branch to use.
# If not set then script defaults to 'develop' branch.
# =========================================================================== #
@douglascayers
douglascayers / delete-local-branches.sh
Last active Sep 6, 2019
Delete local and remote branches
View delete-local-branches.sh
# You'll need to keep one branch.
# Specify that branch's name here.
BRANCH_TO_KEEP=master
# Switch to the branch you're keeping
git checkout ${BRANCH_TO_KEEP}
# Delete all other local branches
git branch -D $(git branch | grep -v -e ${BRANCH_TO_KEEP})
@douglascayers
douglascayers / delete-org.sh
Created Aug 30, 2019
Deletes a scratch org when `force:org:delete` fails for any reason
View delete-org.sh
org_list_json=$(sfdx force:org:list --json)
scratch_org_username=$1
devhub_username=$2
# if no scratch org username given, use default
if [ -z "$1" ]; then
scratch_org_username=$(echo $org_list_json | jq -r '.result.scratchOrgs[] | select(.isDefaultUsername) | .signupUsername')
else
# else check if argument is an org alias and get signup username from that
scratch_org_username=$(echo $org_list_json | jq -r --arg ALIAS "$1" '.result.scratchOrgs[] | select(.alias==$ALIAS) | .signupUsername')
@douglascayers
douglascayers / rename.sh
Created Aug 18, 2019
Rename files in all subdirectories.
View rename.sh
# Finds all files in the current directory and subdirectories
# whose filenames match the expression "*.js" (ends with .js)
# then renames the files, changing their extension to ".ts".
# https://stackoverflow.com/questions/7450818/rename-all-files-in-directory-from-filename-h-to-filename-half
for file in $(find . -type f -name "*.js")
do
mv "$file" "${file/.js/.ts}"
done
@douglascayers
douglascayers / load_properties.sh
Created Aug 17, 2019
Bash: Read a properties file and set variables
View load_properties.sh
# Read a property file line by line and
# set key=value pairs as bash variables
# https://stackoverflow.com/a/28831442/470818
FILEPATH=$1
while IFS='=' read -r k v; do
eval ${k}=${v}
done < $FILEPATH
@douglascayers
douglascayers / github-copy-labels.sh
Last active Sep 26, 2019
Export and import GitHub labels between projects by running bash script with jq and curl. Uses GitHub REST API. Requires personal access token.
View github-copy-labels.sh
# This script uses the GitHub Labels REST API
# https://developer.github.com/v3/issues/labels/
# Provide a personal access token that can
# access the source and target repositories.
# This is how you authorize with the GitHub API.
# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line
GH_TOKEN=
# The source repository whose labels to copy.
@douglascayers
douglascayers / github-export-labels.js
Last active Jul 28, 2019
Export and import GitHub labels between projects by running JavaScript in the browser console to automate clicks.
View github-export-labels.js
/**
* Inspired by @MoOx original script: https://gist.github.com/MoOx/93c2853fee760f42d97f
* Adds file download per @micalevisk https://gist.github.com/MoOx/93c2853fee760f42d97f#gistcomment-2660220
*
* Changes include:
* - Get the description from the `title` attribute instead of `aria-label` (doesn't exist anymore)
* - Use style.backgroundColor and parse the rgb(...) to hex (rather than regex parsing of 'style' string)
* - Downloads labels to a JSON file named after the webpage to know which GitHub repo they came from.
*
* Last tested 2019-July-27:
@douglascayers
douglascayers / DemoBatchJob.cls
Last active Jul 4, 2019
Demo of Apex Batch Job's finish method not running if exception occurs in its execute method (API v46.0)
View DemoBatchJob.cls
public class DemoBatchJob implements Database.Batchable<SObject> {
public Database.QueryLocator start( Database.BatchableContext context ) {
System.debug( 'DemoBatchJob.start: ' + context );
return Database.getQueryLocator([ SELECT Id FROM Organization ]);
}
public void execute( Database.BatchableContext context, List<SObject> records ) {
System.debug( 'DemoBatchJob.execute: ' + context );
Integer i = 1 / 0; // cause exception
@douglascayers
douglascayers / event-start-time-utc.txt
Created Jun 23, 2019
Format Event Start Time in UTC in ISO-8601 Format (no dashes)
View event-start-time-utc.txt
TEXT( YEAR( ActivityDate ) ) &
LPAD( TEXT( MONTH( ActivityDate ) ), 2, '0' ) &
LPAD( TEXT( DAY( ActivityDate ) ), 2, '0' ) &
'T' &
LPAD( TEXT( HOUR( TIMEVALUE( ActivityDateTime ) ) ), 2, '0' ) &
LPAD( TEXT( MINUTE( TIMEVALUE( ActivityDateTime ) ) ), 2, '0' ) &
LPAD( TEXT( SECOND( TIMEVALUE( ActivityDateTime ) ) ), 2, '0' ) &
'Z';
You can’t perform that action at this time.