0

I have a custom object called Account_Addresses__c that has a master-detail relationship to the Account object. That object houses address information as each account may have multiple addresses, however, only one of those addresses can be marked "current". I have a field on the custom object called Current__c to capture which address is current.

I created a trigger (below) to mark and unmark the addresses whena user changes the Current__c field and I am trying (in the same trigger) to then update a checkbox field on the Account record (Left_CA__c) if the newly selected current address is not in California and the Left_CA__c field on the Account was not already checked off. However, this part of my trigger does not seem to be firing properly. From the logs, it looks like my Account map is initially null but then somehow gets populated. What am I doing wrong?

DEBUG|@@@### - parentAcct Map: {} AcctAddrPrimCurr on Account_Addresses trigger event AfterUpdate|__sfdc_trigger/AcctAddrPrimCurr 0 DEBUG|@@@### - parentAcct Map: {0011h00000lC9vvAAC=Account:{Id=0011h00000lC9vvAAC, Left_NY__c=false}}

trigger AcctCurr on Account_Addresses__c(after update,after insert) {
    
    
    //Update Account Address for Current
        List<Account_Addresses__c> acctAddr2 = new List<Account_Addresses__c>();
    
    // Sets holding Account IDs (unique) and Addresses IDs (unique)
        Set<Id> CurrAcctIds = new Set<Id>();
        Set<Id> acctAddrIds2 = new Set<Id>();
    
        for(Account_Addresses__c aa2 : trigger.new){
           if(aa2.Current__c == TRUE){
                CurrAcctIds.add(aa2.Account__c);
                acctAddrIds2.add(aa2.id);
            }
        }
    
    // get the records from Account Addresses that are under the Account but not meant to be Current
    
        acctAddr2 = [SELECT Id,Current__c
                    FROM Account_Addresses__c
                    WHERE Current__c = TRUE AND Account__c IN:CurrAcctIds AND Id NOT IN:acctAddrIds2];
        for(Account_Addresses__c aa2:acctAddr2)
            aa2.Current__c = FALSE;
        update acctAddr2;
    
    //update related Account to check if new current address is not CA
        Map<ID, Account> parentAcct = new Map<ID, Account>();
    
        parentAcct = new Map<Id, Account>([SELECT Id, Left_CA__c, Date_Left_CA__c 
                                           FROM Account 
                                           WHERE Id IN :CurrAcctIds AND Left_CA__c != TRUE]);
    system.debug('@@@### - parentAcct Map:  '+parentAcct);
        if(parentAcct != null){
        
        for(Account_Addresses__c ca: trigger.new){
            Account myParentAcct = parentAcct.get(ca.Account__c);
            if(ca.Current__c == TRUE && ca.State__c != 'CA'){
                myParentAcct.Left_CA__C = TRUE;
                myParentAcct.Date_Left_CA__C = system.TODAY();
            }
        }
    }
    }
1
  • Are you sure you are looking at the correct log? Log shows a different trigger name as AcctAddrPrimCurr and the log also shows Left_NY__c field value which is not being queried in the map collection of this trigger. Possibly multiple triggers on Account Address object?
    – arut
    Sep 8 '20 at 13:36
1

You're not performing DML on your modified account records.

Related records are never automatically updated as part of another object's trigger.

Right now, you're only making a change to the related Account record in memory. To persist that change, you need to use DML.

1
  • Hi Derek, Thanks so much, I completely missed that piece!
    – J. Neilan
    Sep 8 '20 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.