Skip to content

Instantly share code, notes, and snippets.

@aidan-harding
Last active March 8, 2021 11:10
Show Gist options
  • Save aidan-harding/2ee29ed50ef9178ff3825bd63028a29c to your computer and use it in GitHub Desktop.
Save aidan-harding/2ee29ed50ef9178ff3825bd63028a29c to your computer and use it in GitHub Desktop.
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 += '!';
}
}
}
}
@IsTest
private class AccountExclamationTest {
static Account testAccount = new Account(Name = 'ACME');
static {
insert testAccount;
}
@IsTest
static void standardBehaviour() {
testAccount = [SELECT Name FROM Account];
System.assert(!testAccount.Name.endsWith('!'));
update testAccount;
testAccount = [SELECT Name FROM Account];
System.assert(testAccount.Name.endsWith('!'));
}
@IsTest
static void withRollback() {
testAccount = [SELECT Name FROM Account];
System.assert(!testAccount.Name.endsWith('!'));
Savepoint sp = Database.setSavepoint();
update testAccount;
Database.rollback(sp);
update testAccount;
testAccount = [SELECT Name FROM Account];
System.assert(testAccount.Name.endsWith('!')); // FAILS
}
@IsTest
static void allOrNothingFalse() {
testAccount = [SELECT Name FROM Account];
System.assert(!testAccount.Name.endsWith('!'));
Database.update(new Account(Id = testAccount.Id, ParentId = testAccount.Id), false);
update testAccount;
testAccount = [SELECT Name FROM Account];
System.assert(testAccount.Name.endsWith('!')); // FAILS
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment