I am trying to compare the 'old' value with the 'new' value on a record after it is updated using a TriggerHandler. Below is my code for the TriggerHandler class and also the Trigger.
Specifically, I am trying to update the Account name when a checkbox field on the Account is changed. This checkbox is updated by a Trigger on the AccountContactRelation, which is why I am trying to keep it in the same trigger.
I have been trying to test this but for some reason cannot get it working. I also added some system.debug() methods earlier and these are not triggering either in my debug logs. Am I doing something wrong here? Thank you.
public void updateHHDefaultTeam(List<AccountContactRelation> newList, Map<Id, AccountContactRelation> oldMap) {
Set<Id> accountIdsToUpdate = new Set<Id>();
for (AccountContactRelation acr : newList) {
Account relatedAccount = [SELECT Id, Client_Household__c FROM Account WHERE Id = :acr.AccountId];
if (oldMap.get(acr.Id).Account.Client_Household__c != acr.Account.Client_Household__c) {
accountIdsToUpdate.add(relatedAccount.Id);
}
}
if (!accountIdsToUpdate.isEmpty()) {
List<Account> accountsToUpdate = [SELECT Id, Name FROM Account WHERE Id IN :accountIdsToUpdate];
for (Account a : accountsToUpdate) {
a.Name = 'Test123';
}
update accountsToUpdate;
}
}
Here is the Trigger:
trigger AccountContactRelationTrigger on AccountContactRelation (after insert, after update, after delete) {
AccountContactRelationTriggerHandler();
AccountContactRelTriggerHandler handler = new AccountContactRelTriggerHandler();
switch on Trigger.OperationType {
when AFTER_UPDATE{
handler.updateHHDefaultTeam(Trigger.new, Trigger.oldMap);
}