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
B2AB2A 

Index out of bounds message (only when referencing account ID from custom object)

Hi, I am testing a trigger that seems to work only when both objects are standard (date on contact record updates date on account record which is parent).

 

However, when I try the same thing with custom object being child I get an error when saving the custom object record (Meeting) ... below is the code  (the area highlighted in red is the problem area)

 

trigger UpdateAccount_fromMeetingDate on Meetings__c (after insert, after update) { //Triger to test update of relatwed records based on child trigger integer MyCounter=0; Set<Id> Ids = new Set<Id>(); For (Meetings__c NewRecord : trigger.new){ Ids.add(NewRecord.Account__r.id); } List<Account> Account_list = [Select Id,Name, LastMeeting__c From Account Where Id IN :Ids ]; For (Meetings__c MyItem : trigger.new){ Account_list[MyCounter].LastMeeting__c = MyItem.MeetingDate__c; MyCounter++; } update(Account_list); }

The error I get is "System.ListException: List index out of bounds: 0: Trigger.UpdateAccount_fromMeetingDate: line 27, column 22.

 

 

 

When I was using std object (Contact) I never had a problem with indexing and I was using accountID to ref Account (eg. NewRecord.AccountID), rather than what I had to do with a custom object (NewRecord.Account__r.id).   I think the problem is rooting with the ID not saving to the list and thus not having a valid array index existing?

 

Any ideas on ?  I think i'm close but maybe not :S

B2AB2A

 

I got it to work!  The code is the same, however, the main difference is that I added a hidden field the the custom object which is of type formula and from there I got the accountID (accountID__c). 

 

the formula field just does the work of getting the related accounts ID and it seems to do the trick.  I think this is a workaround but not the most elegant fix. 

 

So i'm using

"Ids.add(NewRecord.AccountID__c);" and it makes all the difference.

 

it sucks how "Ids.add(NewRecord.Account__r.id)" does work, which would of made this way less time consuming!

 

Please, any ideas on why this is happening in the first place?