0

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...

4
  • 1
    There's no method to re-enable it?
    – Adrian Larson
    Commented May 12, 2020 at 13:12
  • Yes, there is the enableAfterUpdate, but then it fires when I do insert operations :( which it is what I don't want.. Commented May 12, 2020 at 13:13
  • You need to bookend the insert operation. Disable > Insert > Enable > Update.
    – Adrian Larson
    Commented May 12, 2020 at 13:15
  • Right, did that on the Test class and now it runs over the onAfterUpdate but it is throwing an error : INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] .... thanks.. if you post an answer I will select it as the correct one.. Commented May 12, 2020 at 13:48

1 Answer 1

2

When you bypass trigger logic, you need to bookend your disablement. Typical flow is as below:

@IsTest static void myTest()
{
    // disable trigger
    // insert data
    // enable trigger

    Test.startTest();
        // update data
    Test.stopTest();

    // assert against behavior
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .