0

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

        }
3
  • 1
    You should explain what you're trying to do. Your code won't work, but we don't know what you're trying to do.
    – sfdcfox
    Feb 17, 2023 at 0:52
  • Put another way, It's always good to explain what "it's not working" actually means. Including details is really important because we can't read your mind or log into your org. Same with any error messages you might be getting. If you are getting an error, you need to include the full text of the error (and the stack trace), verbatim.
    – Derek F
    Feb 17, 2023 at 1:01
  • As a side note, that query inside the for loop is a bad idea. Gather the Ids first (in a loop), then you can query (outside of all loops). If you store the results of the query in a map, then it could be referenced inside of the loop over ACRs.
    – Derek F
    Feb 17, 2023 at 1:05

1 Answer 1

3

In the trigger context, you can reach only fields from the processed object level. This means no relation fields are available. So parent fields are not available to compare unless we query it using a SOQL.

Also, If you want to track the changes for the field Account.Client_Household__c then you might need to shift your trigger to the Account object. You will not be able to track the account field changes on the AccountContactRelation object.

1
  • Yep, hit the nail on the head. No related data in TCVs and the trigger needs to be on Account, not on AccountContactRelation.
    – Derek F
    Feb 17, 2023 at 1:07

You must log in to answer this question.

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