I'm trying to use fflib_SObjectDomain.getTriggerEvent(Accounts.class).disableAfterUpdate();
on my Account Domain Class to stop after updates operations to be fired from after insert operations for the account object.
The reason is that some of the methods are executing the same logic on afterInsert
and afterUpdate
for that domain class. I just want to avoid re-executing the same logic when it is not necessary. For this reason I'm using fflib_SObjectDomain.getTriggerEvent(Accounts.class).disableAfterUpdate();
as you can see below:
/**
* After Insert context operations
*
**/
public override void onAfterInsert() {
List<Account> newList = (List<Account>) Records;
Map<Id, Account> oldMap = (Map<Id, Account>) ExistingRecords;
List<Id> accountIds = new List<Id>();
for (Account account : (List<Account>) Records) {
accountIds.add(account.Id);
}
fflib_SObjectDomain.getTriggerEvent(Accounts.class).disableAfterUpdate();
AccountService.sumChildAccountFleetFields(newList, oldMap);
AccountService.signUpForWeeklyDigestAndCampaignFor(newList);
}
/**
* After Update context operations
*
**/
public override void onAfterUpdate(Map<Id, SObject> existingRecords) {
List<Account> newList = (List<Account>) Records;
Map<Id, Account> oldMap = (Map<Id, Account>) existingRecords;
AccountService.sumChildAccountFleetFields(newList, oldMap);
AccountService.signUpForWeeklyDigestAndCampaignFor(newList);
AccountService.listUnsubscribe(newList);
validatePersonEmail(Records);
uow.commitWork();
Now, the issue is that when I try to execute a test method to update a number of accounts, the onAfterUpdate
method never gets called.
I did some tests on the UI to fired a simple email validation method which it is part of the onAfterUpdate
method:
private static void validatePersonEmail(List<SObject> accountsList) {
for (Account account : (List<Account>) accountsList) {
if (String.isBlank(account.PersonEmail)) {
account.addError('You must provide an Email for this account');
}
}
}
That works, so on a real scenario the after update is working.... but how can I test this?
I tried several things but the tests never run over the Account Domain class onAfterUpdate
method. So any tips on this would be great...