function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Maf_007Maf_007 

Trigger to compare Activity Date on Contact throws error

Hi All,

 

I am writing a trigger on Contact to update a date field on account depending on a date field on all related contacts. I am getting a List out of bound  exception: 4

 

Could I please have some guidance on how to rectify this issue? My code is below:

 

//Method to process Trigger.new context for after trigger
    public void LastActivityDateUpdate(List<Contact> updatedcontacts){
        Set<id> contactids = new Set<id>{};
        
        for(Contact con:updatedcontacts){
            if(con.Last_Activity_Date__c != null){
        		contactids.add(con.AccountId);
            }
        }
        
        List<Account> Accwithdateupdate = [Select id, Last_Modified_Date__c, (select id, Last_Activity_Date__c from Contacts) from Account where id in: contactids];
        DateTime lastactivitydateholder;
        
        for(Account a : Accwithdateupdate){
            
            for(Contact c: a.contacts){
                for(integer i = 0; i<a.contacts.size(); i++){
                    lastactivitydateholder = (a.contacts[i].Last_Activity_Date__c > a.contacts[i+1].Last_Activity_Date__c) ? a.contacts[i].Last_Activity_Date__c : a.contacts[i+1].Last_Activity_Date__c;
					//lastactivitydateholder = c.LastActivityDate;
                	//lastactivitydateholder1 = lastactivitydateholder;
                }
            }
            a.Last_Modified_Date__c = lastactivitydateholder;
        }
        update Accwithdateupdate;
    }

 

Thanks in Advance.

Best Answer chosen by Admin (Salesforce Developers) 
Maf_007Maf_007

Hi,

 

Changing my for loop to the following gets rid of the error and but would be grateful if anyone could verify if it's gonna iterate through all the records? Or it's gonna exclude the last record?

 

 for(Account a : Accwithdateupdate){
            for(integer i = 0; i<a.contacts.size()-1; i++){
            //for(Contact c: a.contacts){
                
                    lastactivitydateholder = (a.contacts[i].Last_Activity_Date__c > a.contacts[i+1].Last_Activity_Date__c) ? a.contacts[i].Last_Activity_Date__c : a.contacts[i+1].Last_Activity_Date__c;
					//lastactivitydateholder = c.LastActivityDate;
                	//lastactivitydateholder1 = lastactivitydateholder;
                //}
            }
            a.Last_Modified_Date__c = lastactivitydateholder;
        }

 

All Answers

Maf_007Maf_007

Hi,

 

Changing my for loop to the following gets rid of the error and but would be grateful if anyone could verify if it's gonna iterate through all the records? Or it's gonna exclude the last record?

 

 for(Account a : Accwithdateupdate){
            for(integer i = 0; i<a.contacts.size()-1; i++){
            //for(Contact c: a.contacts){
                
                    lastactivitydateholder = (a.contacts[i].Last_Activity_Date__c > a.contacts[i+1].Last_Activity_Date__c) ? a.contacts[i].Last_Activity_Date__c : a.contacts[i+1].Last_Activity_Date__c;
					//lastactivitydateholder = c.LastActivityDate;
                	//lastactivitydateholder1 = lastactivitydateholder;
                //}
            }
            a.Last_Modified_Date__c = lastactivitydateholder;
        }

 

This was selected as the best answer
Maf_007Maf_007

Hi All,

 

I got the code working with a bit of tweak in for loop. But I am facing issues when trigger is delete. I am using trigger.old context when is delete but it still includes deleted reord activity date and update parent account but I want to exclude deleted activity date from trigger context. How can I achieve that?

 

Any help will be appreciated....