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
sfdclivesfdclive 

System.ListException: List index out of bounds: 0

System.ListException: List index out of bounds: 0

Error is in expression '{!send}' in component <apex:commandButton> in page emailprocessingauto


    Messaging.singleEmailMessage email = new Messaging.singleEmailMessage();
    EmailTemplate Templates = [Select Id, Name, IsActive, Folder.Name,subject
                               From EmailTemplate  where Folder.Name ='DRM Email Templates' and id = :emailtemplateid];
   
    email.setToAddresses( toAddresses );

email.setTemplateID(Templates.Id);
email.settargetObjectId(contactList[0].Id);

 Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});
return null;

 

 

please tell me how to resolve it .

Siddhant IndraSiddhant Indra

Messaging.singleEmailMessage email = new Messaging.singleEmailMessage();


    List<EmailTemplate> TemplatesList = [Select Id, Name, IsActive, Folder.Name,subject
                               From EmailTemplate  where Folder.Name ='DRM Email Templates' and id = :emailtemplateid];

   

    EmailTemplate Templates;

    

   if(TemplatesList != null && TemplatesList.size() !=0)

       Templates = TemplatesList[0];
   
     email.setToAddresses( toAddresses );
    

     if(Templates != null)
        email.setTemplateID(Templates.Id);
    

    if(contactList != null && contactList.size() !=0)

        email.settargetObjectId(contactList[0].Id);

    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});
    return null;

 

 

Your contactList is not having records thats why its giving error. This will resolve your error.

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

List index out of bound exception is shown because you are trying to use the list and referring an id from it.

So, your code should be altered like the following,

 

public PageReference send(){

   List<EmailTemplate> TemplatesList = [Select Id, Name, IsActive, Folder.Name,subject From EmailTemplate

                                                                   where Folder.Name ='DRM Email Templates' and id = :emailtemplateid];

   if(TemplatesList.size() > 0){

     for(Templates t : TemplatesList){
         Messaging.singleEmailMessage email = new Messaging.singleEmailMessage();
         email.setToAddresses( toAddresses );
         email.setTemplateID(t.Id);
         email.settargetObjectId(contactList[0].Id);
         Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});

      }

   }

else{

     //Your error message to show here

}

return null;

}

 

Hope so this helps you...!

 

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.