I have a custom business logic that will block certain profiles from editing/creating/deleting an account.
Because of other requirements, mainly around Record Types, I can't use profiles and permission sets, so I had to use triggers.
My trigger works as expected on the UI, but my unit tests fail when I catch the exception since Database.rollback(sp)
didn't actually rollback my transactions when the exception is caught.
Why is the Account.Phone not not rolled back to null?
void testSomething() {
for(Account acct : accts) {
System.assertEquals(null, acct.Phone); => true
}
System.runAs(UserA) {
System.assertEquals(true, updateAccounts(accts)); => true
}
for(Account acct : accts) {
System.assertEquals(null, acct.Phone); => fails since acct.Phone = (201) 867-5309
}
}
Boolean updateAccounts(acctsToUpdate) {
Boolean errorThrown = false;
Savepoint sp = Database.setSavepoint();
for (Account acct : acctsToUpdate) {
acct.Phone = '(201) 867-5309';
}
try {
update acctsToUpdate;
} catch(Exception e) {
errorThrown = e.getMessage().contains('Some Error');
} finally {
Database.rollback(sp);
}
return errorThrown;
}