0

We have a method in Trigger handler class to update a field on parent record with a value from child record. This method is being called in a Before trigger but this trigger is not firing in some occasions and i couldn't figure out why? Can someone tell me reasons for a trigger to not to be fired? Thank you!

Method in Handler Class:

public static void updateAccountLicenseExpiration(Map<Id, Account> accountMap) {
    Map<Id, Account> accountLicMap = new Map<Id, Account> ([SELECT Id,
    (SELECT Id, Date__c FROM Child_Object__r
     WHERE Status__c IN('Active', 'Suspended', 'Expired')
     AND Date__c != null
     AND(NOT Type__c IN('Salesperson', 'Broker', 'Vendor Representative'))
     ORDER BY Date__c DESC LIMIT 1)
                                                           FROM ACCOUNT
                                                           WHERE ID IN :accountMap.keySet()]);
                                                           system.debug('Account Lic Map size: '+accountLicMap.size());
    for (Account acc : accountLicMap.values()) {
        if (acc.Child_Object__r.size() > 0 && acc.Child_Object__r[0].Date__c != null) {
            accountMap.get(acc.Id).Year__c = String.valueOf(acc.Child_Object__r[0].Date__c.year());
        }
    }
    if (Trigger.isExecuting && Trigger.isBefore) {
        return;
    } else {
        update accountMap.values();
    }
}

Before Trigger

 trigger AccountBeforeTriggerHandler on Account(before delete, before insert, before update) {
    if (Trigger.isInsert || Trigger.isUpdate) {

        if (Trigger.isUpdate) {

            if (AccountTriggerHandler.updateAccountExpiration) {
                               AccountTriggerHandler.updateAccountLicenseExpiration(Trigger.newMap);
          AccountTriggerHandler.updateAccountExpiration = false;

            }
        }
    }
}
1
  • There's no way a trigger cannot fire if it's written to handle specific operations and that a record undergoes through that operation. In your case, there's flag that's being checked before calling your handler method, that's what you need to look for.
    – Jayant Das
    Commented Oct 23, 2018 at 21:00

1 Answer 1

2

The section of code shown below from your handler would seem to explain why:

if (Trigger.isExecuting && Trigger.isBefore) {
        return;
    } else {
        update accountMap.values();
    }

Any time you have a Before Trigger, the handler will simply return without modifying the accountMap.values(). It's only in an After context that your trigger will do an update to the accountMap.values()

0

You must log in to answer this question.

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