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();
}
}
}
}
AcctAddrPrimCurr
and the log also showsLeft_NY__c
field value which is not being queried in the map collection of this trigger. Possibly multiple triggers on Account Address object?