Skip to content

Instantly share code, notes, and snippets.

View SpenceDiNicolantonio's full-sized avatar

Spence DiNicolantonio SpenceDiNicolantonio

View GitHub Profile
@SpenceDiNicolantonio
SpenceDiNicolantonio / download-embedded-vimeo-video.js
Last active August 28, 2021 16:31
[Vimeo Download] Downloads an embedded Vimeo video #browser
downloadEmbededVideos();
/**
* Finds and downloads all embeded Vimeo videos.
*/
function downloadEmbededVideos() {
// Find Vimeo embed frame
var embedFrames = document.querySelectorAll('iframe[src*="player.vimeo.com"]');
@SpenceDiNicolantonio
SpenceDiNicolantonio / get-translation.js
Last active February 2, 2019 07:00
[CSS Translation Extract] Function to extract translation values from CSS transform value #css
/**
* Extracts the translation values from a given CSS transformation.
* @param {string} transformValue The CSS transform string from which the translation values will be extracted (e.g. ‘translate3d(10px, 25px, 30px)’)
* @returns {object} translation values found in the given transform (e.g. { x: 10, y: 25, z: 30 })
*/
function getTranslation(transformValue) {
var matches = transformValue.match(/translate(3d)*\((\d+.?\d*(px)?),\s*(\d+.?\d*(px)?)(,\s*(\d+.?\d*(px)?))?\)/);
if (matches) {
return {
x: parseInt(matches[2]) || 0,
@SpenceDiNicolantonio
SpenceDiNicolantonio / DailyLeadProcessor.cls
Created February 2, 2019 07:58
[Scheduled Apex] Example of a schedulable Apex job #salesforce #apex
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext context) {
List<Lead> leads = [Select Id, LeadSource from Lead where LeadSource = null limit 200];
for (Lead currentLead : leads) {
currentLead.LeadSource = 'Dreamforce';
}
update leads;
}
}
@SpenceDiNicolantonio
SpenceDiNicolantonio / Mass-Deactivate Salesforce Users
Last active January 26, 2021 21:20
[Mass-Deactivate Salesforce Users] Deactivates all users that aren't assigned one of the profiles definied in a set #salesforce #apex
// Profiles to leave enabled
Set<String> excludedProfiles = new Set<String> {
'System Administrator',
'Integration System Administrator',
'AccessTSC Digital Profile',
'TriState Onboarding Team',
'Tristate Administrator'
};
List<User> usersToUpdate = new List<User>();
@SpenceDiNicolantonio
SpenceDiNicolantonio / getType.cls
Created April 3, 2019 16:52
[Get instance type in Apex] A hack to determine the type of an instance in Apex #salesforce #apex
/*
* Returns the type of a given object. This is a hack around the fact that we can't access the type of an object.
* @param {Object} obj An object
* @returns {Type} the type of the object
*/
private static Type getType(Object obj) {
String typeName = 'Date';
// Attempt to cast the object to Datetime
// If it succeeds, the object is a date
@SpenceDiNicolantonio
SpenceDiNicolantonio / JsonNode.cls
Created July 5, 2019 18:41
[Traversable JSON (and XML) in Apex] Simulated tree structure to represent JSON or XML in a traversable and queriable way #salesforce #apex
/*
* A traversable JSON data structure.
*
* Conceptually, JSON is a general tree, or array of general trees. It is represented in Apex using Map<String, Object>
* for objects, List<Object> for arrays, String for strings, and either Integer or Decimal for numbers. This class wraps
* these data types in order to provide an interface for traversiing and querying the entire JSON structure.
*
* Each node contains a single value that must be a valid JSON value. That is, Map<String, Object>, List<Object>,
* String, Decimal, Integer, or null. When traversing or querying a JSON structure, any value returned is always wrapped
* in a JsonData instance to allow subsequent traversal or query.
@SpenceDiNicolantonio
SpenceDiNicolantonio / Uuid.cls
Created July 5, 2019 19:04
[UUID Generator in Apex] #salesforce #apex
/**
* A UUID generator that can construct unique IDs in a variety of formats.
*
* A UUID is generated immediately upon instantiation of this class. The UUID can be retrieved in its normal form
* (e.g. f111b8c5-ca2f-4a1a-8d0d-a8dd5f37c05f) or as a shortened web-safe form (e.g. jp64hwPZ-Lh7vY8INQA7ImQPbQE), which
* is constructed by converting the UUID to Base64 and replacing '/' and '+' with '-' and '_', respectively.
*/
public class Uuid {
private static final String HEX_PREFIX = '0x';
@SpenceDiNicolantonio
SpenceDiNicolantonio / HttpMockRegistry.cls
Last active July 29, 2022 18:03
[More Robust Mocking in Apex] A dynamic HTTP mock registry and a configurable Stub class to simplify and enhance mocking for Apex unit tests #salesforce #apex
/**
* A registry built around the Salesforce Mocking API that allows declarative mocking of HTTP callouts. Mocks responses
* can be registered either for a specific endpoint and path or for all paths on an endpoint, with the former taking
* precedence.
*/
@IsTest
public class HttpMockRegistry {
// Default mock response for HTTP requests
public static final HttpResponse DEFAULT_MOCK_RESPONSE = createSuccessResponse('Default mock response');
@SpenceDiNicolantonio
SpenceDiNicolantonio / TestUtil.cls
Last active December 20, 2023 19:29
[Salesforce ID generator] Generates 15-character Salesforce IDs for testing #salesforce #apex
/**
* A collection of static utility methods for unit testing.
*/
@IsTest
public class TestUtil {
// ID generator configuration
private static final Integer ID_RESERVED_CHARACTERS = 7;
private static final Integer ID_INCREMENTER_PREFIX_LENGTH = 1;
private static final String BASE62_ALPHABET = '0123456789abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
@SpenceDiNicolantonio
SpenceDiNicolantonio / tsv-to-json.js
Created May 27, 2021 13:14
[TSV to JSON] Convert a tab-separated-values file's contents to a set of JSON objects #json #tsv #lodash
import _ from 'lodash';
/**
* Converts a tab-separted-values (TSV) file to a set of JSON objects
* where each value is keyed according to the heading row
*/
const tsvToJson = tsv => {
// Split lines and filter any empties
const lines = tsv
.split('\n') // Split lines