Last active
March 8, 2021 11:10
-
-
Save aidan-harding/2ee29ed50ef9178ff3825bd63028a29c to your computer and use it in GitHub Desktop.
Trigger recursion control with static variables fails with roll-backs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 += '!'; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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