Skip to content

Instantly share code, notes, and snippets.

View aidan-harding's full-sized avatar

Aidan Harding aidan-harding

  • Nebula Consulting
View GitHub Profile
@aidan-harding
aidan-harding / set_behaviour.apex
Last active January 29, 2018 20:03
Strange Set Behaviour in Apex
Integer nCallsToEquals = 0;
public class HashingTest {
private String s;
public HashingTest(HashingTest ht) {
this.s = ht.s;
}
public HashingTest(String s) {
String jsonPayload = new YouTubeApi().getPlaylistThumbnail('playlist-id');
String thumbnailUrl = new JsonReader(jsonPayload)
.read('items[0].snippet.thumbnails.default.url'));
@aidan-harding
aidan-harding / AccountExclamation.cls
Last active March 8, 2021 11:10
Trigger recursion control with static variables fails with roll-backs
public with sharing class AccountExclamation implements TriggerAction.BeforeUpdate {
public void beforeUpdate(List<Account> newList, List<Account> oldList) {
for(Account thisAccount : newList) {
if(TriggerBase.idToNumberOfTimesSeenBeforeUpdate.get(thisAccount.Id) == 1 && !thisAccount.Name.endsWith('!')) {
thisAccount.Name += '!';
}
}
}
}
function sleeper(value) {
return new Promise(resolve => setTimeout(() => resolve(value), value * 100));
}
[5, 4, 3, 6, 1].forEach((value) => sleeper(value).then((value) => console.log(value)));
#!/bin/zsh
#
# Check that sfdx force:source:status is clean before committing
# Put this into .git/hooks/pre-commit
if ! sfdx force:source:status --json | jq -r '.result | length' | grep '^0$' &> /dev/null;
then
echo "Error: sfdx source is not synced with remote"
exit 1
fi
@aidan-harding
aidan-harding / rules.xml
Created August 2, 2021 10:15
XPath PMD rule to check that Salesforce custom fields contain no underscores
<?xml version="1.0"?>
<ruleset name="Salesforce Custom Rules"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
My Salesforce rules
</description>
class MyClass {
constructor() {
const self = this;
Object.getOwnPropertyNames( MyClass.prototype )
.filter(name => name.indexOf('_') === 0)
.filter(name => typeof self[name] === 'function')
.forEach(function(name) {
self[name.substr(1)] = function() {
self[name].apply(self, arguments);
}
<template>
<lightning-datatable
key-field="id"
columns={columns}
data={data}
hide-checkbox-column
default-sort-direction={dataTableSorter.defaultSortDirection}
sorted-direction={dataTableSorter.sortDirection}
sorted-by={dataTableSorter.sortedBy}
onsort={dataTableSorter.sortData}>
@aidan-harding
aidan-harding / TransformationTest.cls
Created November 16, 2021 15:57
Tests for a CMDT-driven Apex object transformation library
/**
* @author aidan@nebulaconsulting.co.uk
* @date 15/11/2021
*/
@IsTest
private class TransformationTest {
@IsTest
static void passThroughSObjectToOtherSObject() {
@aidan-harding
aidan-harding / LockForDelete.md
Last active January 12, 2022 10:30
Salesforce locks for deleting?

I have a scenario where an external system is calling Salesforce API, causing a trigger to run. That trigger needs to delete EmailMessage records where the RelatedTo field is an Account.

We're getting UNABLE_TO_LOCK_ROW errors when the external system's API calls overlap and the trigger attempts to delete multiple emails on the same Account at the same time.

This is totally understandable based on how [record-locking works][1]. RelatedTo is either a required lookup or a Master-Detail so deleting the EmailMessage would require a lock on the related Account.

I tried to solve the problem by adding FOR UPDATE to the query in the trigger where it fetches the EmailMessage records.

But, we still observe UNABLE_TO_LOCK_ROW as a DmlException (if it were a QueryException, then that might be due to the 10-second limit timing out).