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
nikita dhamalnikita dhamal 

Error :"System.ListException: List index out of bounds: 2" while trying to send email using apex code

I have a requirement that when particular contacts are selected from the contacts list email should be sent to those contacts.but it is giving the above error . my code is:
vf page:
<apex:page controller="ContactSelectClassController" sidebar="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="table2"/>
            </apex:pageBlockButtons>
 
            <apex:pageblockSection title="All Contacts" collapsible="false" columns="2">
 
             <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Contacts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.Email}" />
                    <apex:column value="{!accWrap.acc.Phone}" />
                </apex:pageBlockTable>
 
                <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Accounts">
                   <apex:column value="{!c.Id}" headerValue="Contact"/>
                    <apex:column value="{!c.Name}" headerValue="Contact Name"/>
                    <apex:column value="{!c.Email}" headerValue="Email"/>
                    <apex:column value="{!c.Phone}" headerValue="Phone"/>
                    
                </apex:pageBlockTable>
 
            </apex:pageblockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Send Email" action="{!SendEmail}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

controller:
public class ContactSelectClassController{
 
   
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Contact> selectedAccounts{get;set;}
 
 private  List<Id> contactids=new list<Id>();
  public List<Contact> conts;
  
 

  public void SendEmail()
  {
    
      for(Integer i=0;i<5;i++)
     {
         contactids.add(selectedAccounts[i].Id);
      } 
       Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
       mail.setTargetObjectIds(contactids);
       mail.setTemplateId('00X28000000QfNL');
// the above template id is a valid id of template i have created.
       Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
      
 } 
 
 
 
    public ContactSelectClassController()
    {
        if(wrapAccountList == null)
         {
            wrapAccountList = new List<wrapAccount>();
            for(Contact a: [select Id, Name,Email,Phone from Contact limit 10])
             {
                wrapAccountList.add(new wrapAccount(a));
             }
         }
     }
 
    public void processSelected()
     {
    selectedAccounts = new List<Contact>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList)
         {
            if(wrapAccountObj.selected == true)
             {
                selectedAccounts.add(wrapAccountObj.acc);
                
           
             }
         }
     }
 
    public class wrapAccount {
        public Contact acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Contact a) {
            acc = a;
            selected = false;
        }
    }
}
Best Answer chosen by nikita dhamal
Amit Chaudhary 8Amit Chaudhary 8
try to update your code like below
public class ContactSelectClassController{
 
   
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Contact> selectedAccounts{get;set;}
 
	private  List<Id> contactids=new list<Id>();
	public List<Contact> conts;
  
 

	public void SendEmail()
	{
		for(Contact cont: selectedAccounts)
		{
			contactids.add(cont.Id);
		} 
		Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
		mail.setTargetObjectIds(contactids);
		mail.setTemplateId('00X28000000QfNL');
		Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
	} 
 
 
 
    public ContactSelectClassController()
    {
        if(wrapAccountList == null)
         {
            wrapAccountList = new List<wrapAccount>();
            for(Contact a: [select Id, Name,Email,Phone from Contact limit 10])
             {
                wrapAccountList.add(new wrapAccount(a));
             }
         }
     }
 
    public void processSelected()
     {
    selectedAccounts = new List<Contact>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList)
         {
            if(wrapAccountObj.selected == true)
             {
                selectedAccounts.add(wrapAccountObj.acc);
                
           
             }
         }
     }
 
    public class wrapAccount {
        public Contact acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Contact a) {
            acc = a;
            selected = false;
        }
    }
}
let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
try to update your code like below
public class ContactSelectClassController{
 
   
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Contact> selectedAccounts{get;set;}
 
	private  List<Id> contactids=new list<Id>();
	public List<Contact> conts;
  
 

	public void SendEmail()
	{
		for(Contact cont: selectedAccounts)
		{
			contactids.add(cont.Id);
		} 
		Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
		mail.setTargetObjectIds(contactids);
		mail.setTemplateId('00X28000000QfNL');
		Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
	} 
 
 
 
    public ContactSelectClassController()
    {
        if(wrapAccountList == null)
         {
            wrapAccountList = new List<wrapAccount>();
            for(Contact a: [select Id, Name,Email,Phone from Contact limit 10])
             {
                wrapAccountList.add(new wrapAccount(a));
             }
         }
     }
 
    public void processSelected()
     {
    selectedAccounts = new List<Contact>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList)
         {
            if(wrapAccountObj.selected == true)
             {
                selectedAccounts.add(wrapAccountObj.acc);
                
           
             }
         }
     }
 
    public class wrapAccount {
        public Contact acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Contact a) {
            acc = a;
            selected = false;
        }
    }
}
let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer
nikita dhamalnikita dhamal
Thanks Amit, its working now..