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;
}
}
}
}