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
Angadi VCAngadi VC 

List index out of bounds: 1 in trigger

Hello,

I have trigger on contentdocumentlink where number files added is rollep up on one of the field(Total_no_of_attachment__c) of custom object(promotion__c).But when updating the list,it throwing List index out of bounds: 1  exception.

below is the code:
trigger UploadFilesToPromo on ContentDocumentlink (after insert,after delete,after undelete) {
        List<ContentDocumentLink > data=trigger.isinsert?trigger.new:trigger.old;    
        set<id> promotionid = new set<id>();
        set<id> docId = new set<id>();
        System.debug('data***********'+data+'************');
        
        
        for(ContentDocumentLink att:data)
        {
            promotionid.add(att.LinkedEntityId );
            System.debug('parentID ************'+att.LinkedEntityId +'************');
            System.debug('id in set -promotionid ************'+promotionid);
        }
        
        List<promotion__c> oldpromotionWithAtt = new List<promotion__c>();
        oldpromotionWithAtt =[select id,total_no_of_attachment__c from promotion__c where Id IN:promotionid ];
        for(promotion__c promo : oldpromotionWithAtt) {
            docId.add(promo.Id);
        }
        system.debug('promotionlist.....................................'+oldpromotionWithAtt );
        system.debug('docID set id--------'+docId);
        List<AggregateResult> totalCount= [SELECT count(Id) total FROM ContentDocumentLink WHERE LinkedEntityId IN: docId];
        System.debug('totalcount of files----------------------'+totalCount+'************');
        Integer i=0;
        List<promotion__c> ls = new List<promotion__c>();
        try{
            for(ContentDocumentLink atr:data)
            {
                promotion__c pr=oldpromotionWithAtt.get(i); 
              
                try{
                        pr.Total_No_of_Attachment__c=Integer.valueOf(totalCount[i].get('total'));
                        system.debug('output...............'+pr.Total_No_of_Attachment__c);
                } catch(Exception ex)
                  {    
                        System.debug('exception new   ............'+ex);
                        pr.Total_no_of_attachment__c=0;
                        system.debug('output...............'+pr.Total_no_of_attachment__c);
                  } 
                     i++;
                    ls.add(pr);
                    system.debug('ls list from adding pr'+ls);
                
            }
            
            system.debug('final op============================'+ls);
            if(ls.size()>0 && !ls.IsEmpty()){
                system.debug('ls.size++++++++++++++++++'+ls.size());
                update ls;
            } 
        }
        catch(exception ex1)
        {
            System.debug('exception   ............'+ex1);
        }
        
   }


Thanks in advance.
PrasathPrasath
Hi Angadi,

If you then attempt to access an element at row 0, it'll throw an error as that row doesn't exist. It's good practice to check there are records first in order to make sure that the list is not empty before accessing it.

You are getting error because of the below line,
 
pr.Total_No_of_Attachment__c=Integer.valueOf(totalCount[i].get('total'));

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Prasath
Angadi VCAngadi VC
Hi prasath,

Its still same issue after null check.Please find the changed code

/********************************XXX********************************************************
    
    Description     : To calculate the number of attachments in promotion object for upload files button
    
    **********************************XXX*******************************************************/
    trigger UploadFilesToPromo on ContentDocumentlink (after insert,after delete,after undelete) {
        List<ContentDocumentLink > data=trigger.isinsert?trigger.new:trigger.old;    
        set<id> promotionid = new set<id>();
        set<id> docId = new set<id>();
        System.debug('data***********'+data+'************');
        
        
        for(ContentDocumentLink att:data)
        {
            promotionid.add(att.LinkedEntityId );
            System.debug('parentID ************'+att.LinkedEntityId +'************');
            System.debug('id in set -promotionid ************'+promotionid);
        }
        
        List<promotion__c> oldpromotionWithAtt = new List<promotion__c>();
        oldpromotionWithAtt =[select id,total_no_of_attachment__c from promotion__c where Id IN:promotionid ];
        for(promotion__c promo : oldpromotionWithAtt) {
            docId.add(promo.Id);
        }
        system.debug('promotionlist.....................................'+oldpromotionWithAtt );
        system.debug('docID set id--------'+docId);
        List<AggregateResult> totalCount= [SELECT count(Id) total FROM ContentDocumentLink WHERE LinkedEntityId IN: docId];
        System.debug('totalcount of files----------------------'+totalCount+'************');
        Integer i=0;
        List<promotion__c> ls = new List<promotion__c>();
        try{
            for(ContentDocumentLink atr:data)
            {
                promotion__c pr=oldpromotionWithAtt.get(i); 
              
                try{
                        if(totalcount!=null){
                            pr.Total_No_of_Attachment__c=Integer.valueOf(<u><b>totalCount[i]</b></u>.get('total'));
                            system.debug('output...............'+pr.Total_No_of_Attachment__c);
                        }
                } catch(Exception ex)
                  {    
                        System.debug('exception new   ............'+ex);
                        pr.Total_no_of_attachment__c=0;
                        system.debug('output...............'+pr.Total_no_of_attachment__c);
                  } 
                     i++;
                    ls.add(pr);
                    system.debug('ls list from adding pr'+ls);
                
            }
            
            system.debug('final op============================'+ls);
            if(ls.size()>0 && !ls.IsEmpty()){
                system.debug('ls.size++++++++++++++++++'+ls.size());
                update ls;
            } 
        }
        catch(exception ex1)
        {
            System.debug('exception   ............'+ex1);
        }
        
        
    }
Deepali KulshresthaDeepali Kulshrestha
Hi Angadi,
The reason this error is still shown while processing your code is you have not used null check properly as 
you are checking for a list so you must use if(totalcount.size()>0) instead of using if(totalcount!=null).
  if(totalcount!=null) this is a wrong attempt to check null for a list.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha