Skip to main content

Trigger:

trigger AccountStatusUpdate on Contact (after update) {

System.debug('ACCOUNT STATUS TRIGGER: starting trigger now');

// create list for account Ids

List<Id> accIds = new List<Id>();

// loop through Contacts from the trigger

for (Contact con: Trigger.new){

// if updated Contact is a primary contact

if(con.Primary_Contact__c == true){

// add each Contact's Account to list of Account Ids

accIds.add(con.AccountId);

}

System.debug('ACCOUNT STATUS TRIGGER: here is list of Ids related to a contact you edited: '+accIds);

}

//find map holding Acccounts that have Id included in list of Account Ids created above

Map<Id,Account> accMap = new Map<Id,Account>([SELECT Id, Intro_Call__c, Intro_Call_Date__c, Follow_Up_Date__c, Sent_1st_Access_Date__c FROM Account WHERE Id IN: accIds]);

// create list of Accounts to update at the end of the process

List<Account> accountsToUpdate = new List<Account>();

System.debug('ACCOUNT STATUS TRIGGER: created accountsToUpdate List to update at the end of this process');

// create temp Account for placeholding during the following for loop

Account tempAccount;

System.debug('ACCOUNT STATUS TRIGGER: creating tempAccount');

// loop through the Contacts from the trigger

for (Contact con: Trigger.new){

// check again if primary contact

if(con.Primary_Contact__c == true){

// check for change in Intro Date value

if(Trigger.oldMap.get(con.Id).Intro_Call_Date__c != con.Intro_Call_Date__c){

tempAccount = accMap.get(con.AccountId);

tempAccount.Intro_Call_Date__c = con.Intro_Call_Date__c;

tempAccount.Intro_Call__c = true;

accountsToUpdate.add(tempAccount);

System.debug('ACCOUNT STATUS TRIGGER: adding Intro Call and Intro Call Date values to tempAccount variable');

}

if(Trigger.oldMap.get(con.Id).Intro_FollowUp_Date__c != con.Intro_FollowUp_Date__c){

tempAccount = accMap.get(con.AccountId);

tempAccount.Follow_Up_Date__c= con.Intro_FollowUp_Date__c;

accountsToUpdate.add(tempAccount);

System.debug('ACCOUNT STATUS TRIGGER: adding Followup Date value to tempAccount variable');

}

if(Trigger.oldMap.get(con.Id).Sent_Access__c != con.Sent_Access__c){

tempAccount = accMap.get(con.AccountId);

tempAccount.Sent_1st_Access_Date__c = con.Sent_Access__c;

accountsToUpdate.add(tempAccount);

System.debug('ACCOUNT STATUS TRIGGER: adding Sent1st Access Date values to tempAccount variable');

}

if(Trigger.oldMap.get(con.Id).Contact_Status__c != con.Contact_Status__c){

if(con.Contact_Status__c == 'Active'){

tempAccount = accMap.get(con.AccountId);

tempAccount.Partner_Status__c = 'Active';

accountsToUpdate.add(tempAccount);

System.debug('ACCOUNT STATUS TRIGGER: changing Account status to Active because Primary POC became active');

}

}

}

}

// if this list has any records, then update said records

if(accountsToUpdate.size() > 0){

update accountsToUpdate;

} else

System.debug('ACCOUNT STATUS TRIGGER: no Account to update at this time');

}

 

Test:

 

 

@isTest

public class TestAccountStatusUpdate {

@isTest static void TESTAccountStatusWithUpdatedContactDates(){

Account acct = new Account(Name='Test Account45', Primary_Contact__c='KenTest');

insert acct;

Contact con = new Contact(AccountId=acct.Id, lastName='KenKen', Primary_Contact__c=true);

insert con;

Test.startTest();

con.Intro_Call_Date__c=Date.today()+1;

con.Intro_FollowUP_Date__c=Date.today()+1;

con.Sent_Access__c=Date.today()+1;

con.Contact_Status__c='Active';

update con;

Test.stopTest();

}

}

Error:

System.DmlException: Update failed. First exception on row 0 with id 003r000000L4KLeAAN; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountStatusUpdate: execution of AfterUpdate

caused by: System.ListException: Duplicate id in list: 001r000000I74iXAAR

Trigger.AccountStatusUpdate: line 61, column 1: []

 

​​​​​​​
5 answers
  1. Apr 4, 2020, 3:37 AM
    Hi Ken,

     

    Sorry, what error?

     

    Have you not noticed that the List was changed to a Map?

    Map<Id,Account> accountsToUpdate = new Map<Id,Account>();

     

     
0/9000