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
tantoniotantonio 

BeforeDelete caused by: System.ListException: List index out of bounds: 0

Hi All,

I am having trouble figuring out why this is method id failing. 

The row that is failing is when I try to add the values back into my list. 

Line: deletedChangeRecords.add(deletedChangeRecords[0]);

Error: There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger scv_tfs_marquee caused an unexpected exception, contact your administrator: scv_tfs_marquee: execution of BeforeDelete caused by: System.ListException: List index out of bounds: 0: Class.svc_tfs.MarqueeDelete: line 105, column 1".

I think this has something to do with the fact that this method is called by a before delete trigger. Functionally, this method is supposed to update a record on a different object (ChangeRecord__c) before the delete occurs. 

Thanks for any help on with this!


public static void MarqueeDelete(List<NI_MarqueeFeature__c> marqueeDelete){ 
        
            List <ChangeRecord__c> deletedChangeRecords = new List <ChangeRecord__c>();

        	for (NI_MarqueeFeature__c md : marqueeDelete){//deletedChangeRecords contains trigger.new
            List <ChangeRecord__c>  deletedChangeRecord  = [SELECT ID, LastMod__c FROM ChangeRecord__c WHERE ChangeRecord__c.ID__c = :md.Id LIMIT 1];           
            	
            	//If now matching change record is found, create one
            	//else update the existing one
            	if (deletedChangeRecord.size()<1){
                    ChangeRecord__c dm = new ChangeRecord__c ();

                        dm.ID__c = md.id;
                        dm.ObjectType__c = 0; //0 for Marquee Feature, 1 for CR, 2 for Product Release
                    	dm.type__c = 2;
                        deletedChangeRecord.add(dm);
                        
                        try {
                            insert dm;
                          } catch (system.DmlException e) {
                            system.debug (e);}
                                                
                } else {
            			//Setting the value of LastModd__c to the last mod of trigger.new
                        deletedChangeRecord[0].LastMod__c = md.LastModifiedDate;
                    	deletedChangeRecord[0].type__c = 2;
                    try {
                        deletedChangeRecords.add(deletedChangeRecords[0]);
            		}  catch (system.DmlException e) {
                        system.debug (e);}
        }  
     }


Best Answer chosen by tantonio
tantoniotantonio
I found the issue, I had a type in the line:

Changed 

deletedChangeRecords.add(deletedChangeRecords[0]);

to 

deletedChangeRecords.add(deletedChangeRecord[0]);

Problem solved. Thanks!

All Answers

Ramu_SFDCRamu_SFDC
Add the below condition before this line if (deletedChangeRecord.size()<1)

if(DeletedChangeRecord!=null){
}
tantoniotantonio
Thanks for the answer. I added the condition, still same error :-/

public static void MarqueeDelete(List<NI_MarqueeFeature__c> marqueeDelete){ 
        
            List <ChangeRecord__c> deletedChangeRecords = new List <ChangeRecord__c>();

        	for (NI_MarqueeFeature__c md : marqueeDelete){//deletedChangeRecords contains trigger.new
            List <ChangeRecord__c>  deletedChangeRecord  = [SELECT ID, LastMod__c FROM ChangeRecord__c WHERE ChangeRecord__c.ID__c = :md.Id LIMIT 1];           
            	
            	//If now matching change record is found, create one
            	//else update the existing one
            	if(DeletedChangeRecord!=null){

            	if (deletedChangeRecord.size()<1){
                    ChangeRecord__c dm = new ChangeRecord__c ();

                        dm.ID__c = md.id;
                        dm.ObjectType__c = 0; //0 for Marquee Feature, 1 for CR, 2 for Product Release
                    	dm.type__c = 2;
                        deletedChangeRecord.add(dm);
                        
                        try {
                            insert dm;
                          } catch (system.DmlException e) {
                            system.debug (e);}
                                                
                } else {
            			//Setting the value of LastModd__c to the last mod of trigger.new
                        deletedChangeRecord[0].LastMod__c = md.LastModifiedDate;
                    	deletedChangeRecord[0].type__c = 2;
                    try {
                        deletedChangeRecords.add(deletedChangeRecords[0]);
            		}  catch (system.DmlException e) {
                        system.debug (e);}
                }
        }  
     }


tantoniotantonio
I found the issue, I had a type in the line:

Changed 

deletedChangeRecords.add(deletedChangeRecords[0]);

to 

deletedChangeRecords.add(deletedChangeRecord[0]);

Problem solved. Thanks!
This was selected as the best answer