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
AkshuAkshu 

I need help to solve this error

When click on send mail button:
User-added image*************vf page******************
<apex:page Controller="wrapperClassController">
    <apex:form>
    <head>
       <apex:includescript value="https://code.jquery.com/jquery-1.11.1.min.js" / >
       <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.18/r-2.2.2/rg-1.0.3/sl-1.2.6/datatables.min.css"/>
       <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.18/r-2.2.2/rg-1.0.3/sl-1.2.6/datatables.min.js"></script>
      <link type="text/css" href="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.11/css/dataTables.checkboxes.css" rel="stylesheet" />
      <script type="text/javascript" src="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.11/js/dataTables.checkboxes.min.js"></script>  
        <script>
          j$ = jQuery.noConflict();
         j$(document).ready(function () {
         var conTable = j$('[id$="contacttable"]').DataTable({
                   "pageLength": 50,
                   'columnDefs': [{
                   'targets': 0,
                        'checkboxes': {
                              'selectRow': true
                   },
                        "orderable": false 
                    }
                   ],

               'select': {
               'style': 'multi'
                     }
                  });

            });

        </script>

    </head>
       <apex:pageBlock>
        <apex:pageBlockButtons location="top">
               <apex:outputPanel layout="none" id="buttonsPanel">
                  <apex:commandButton value="send Mail" action="{!sendEmails}"> 
                </apex:commandButton> 
                <apex:commandButton value="Reset" action="{!reset}" immediate="true"/>
               </apex:outputPanel>      
            </apex:pageBlockButtons>
    <body>
        <table id="contacttable" class="display">
            <thead>
                <tr>
                    <th><apex:inputCheckbox/></th>
                    <th>Email</th>
                    <th>Name</th>
                    <th>Phone</th>
                </tr>
            </thead>
            <tbody>
                <apex:repeat value="{!cListWrapper}" var="c">
                    <tr>
                    <td><apex:inputCheckbox value="{!c.selected}" id="inputId"/></td>
                        <td>{!c.con.Email}</td>
                        <td>{!c.con.Name}</td>
                        <td>{!c.con.Phone}</td>
                    </tr>
                </apex:repeat>
            </tbody>
        </table>
     </body>
    </apex:pageBlock>        
   </apex:form>
 </apex:page>
***************************controller**********************
public class wrapperClassController {
       private String recordId;

    public Boolean selected {get; set;}
    public List<Contact> contactList {get;set;}
    public List<cWrapper> cListWrapper {get;set;}

    public wrapperClassController(){
        contactList = [SELECT Id, Name, Phone,Email FROM Contact WHERE AccountId=:recordId limit 1000];
        cListWrapper = new list<cWrapper>();

            for(Contact c: contactList) {
                cListWrapper.add(new cWrapper(c));
            }
    }    
    public class cWrapper {

        public Contact  con {get; set;}
        public Boolean selected {get; set;}


        public cWrapper(Contact  c) {
            con = c;
            selected = false;
        }
    }

    public PageReference reset() {
        PageReference pg = new PageReference(System.currentPageReference().getURL());
        pg.setRedirect(true);
        return pg;
    }

  public PageReference sendEmails(){
         List<Contact> selectedContacts = new List<Contact>();
         //System.debug('==>Inside sendEmail() '+ wrapperConsList);
        for(cWrapper c: cListWrapper) {
            if(c.Selected == true) {
                selectedContacts.add(c.con);

            }
        }
      // System.debug('toaddresses==>'+selectedContacts);
        String []toAddresses = new List<String>();
      //String[] toAddresses = new String[] {};
        
        for(Contact c : selectedContacts) {
            
            toAddresses.add(c.Email);
           
        }
       // System.debug('toaddresses==>'+toAddresses);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddresses);
        mail.setSubject('VF page created' );
        mail.setHtmlBody('This SF message!!!!!!   Thank you');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        //update selectedContacts;
        return null;
    }  

}
 
SwethaSwetha (Salesforce Developers) 
HI Akshu,
From the error message, it is evident that the toAddresses is null. Can you add a debug and see what it fetches for the selectedContacts in the  sendEmails() of the apex class?Thanks
Maharajan CMaharajan C
Hi Aksh,

The issue is coming from below JS file which your importing. It's making some conflict. 
<script type="text/javascript" src="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.11/js/dataTables.checkboxes.min.js"></script>

The above JS overrifding the checkboxes with their own. So i have commented the above line and used our own checkboxes. Added the function select all checkbox also.

VF page:
 
<apex:page Controller="wrapperClassController">
    <apex:form >
    <head>
       <apex:includescript value="https://code.jquery.com/jquery-1.11.1.min.js" / >
       <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.18/r-2.2.2/rg-1.0.3/sl-1.2.6/datatables.min.css"/>
       <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.18/r-2.2.2/rg-1.0.3/sl-1.2.6/datatables.min.js"></script>
       <link type="text/css" href="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.11/css/dataTables.checkboxes.css" rel="stylesheet" /> 
       <!-- <script type="text/javascript" src="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.11/js/dataTables.checkboxes.min.js"></script>   -->
        <script>
          j$ = jQuery.noConflict();
         j$(document).ready(function () {
         var conTable = j$('[id$="contacttable"]').DataTable({
                   "pageLength": 50,
                   'columnDefs': [{
                   'targets': 0,
                        'checkboxes': {
                              'selectRow': true
                   },
                        "orderable": false 
                    }
                   ],

               'select': {
               'style': 'multi'
                     }
                  });

            });

        </script>
           
           <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>

    </head>
       <apex:pageBlock >
        <apex:pageBlockButtons location="top">
               <apex:outputPanel layout="none" id="buttonsPanel">
                  <apex:commandButton value="send Mail" action="{!sendEmails}"> 
                </apex:commandButton> 
                <apex:commandButton value="Reset" action="{!reset}" immediate="true"/>
               </apex:outputPanel>      
            </apex:pageBlockButtons>
    <body>
        <table id="contacttable" class="display">
            <thead>
                <tr>
                    <th><apex:inputCheckbox style="margin-left: -2px;" onclick="selectAllCheckboxes(this,'inputId')"/></th>
                    <th>Email</th>
                    <th>Name</th>
                    <th>Phone</th>
                </tr>
            </thead>
            <tbody>
                <apex:repeat value="{!cListWrapper}" var="c">
                    <tr>
                    <td><apex:inputcheckbox value="{!c.selected}" id="inputId"/></td>

                        <td>{!c.con.Email}</td>
                        <td>{!c.con.Name}</td>
                        <td>{!c.con.Phone}</td>
                    </tr>
                </apex:repeat>
            </tbody>
        </table>
     </body>
    </apex:pageBlock>        
   </apex:form>
 </apex:page>



Class:
 
public class wrapperClassController {
    private String recordId;
    
    //public Boolean selected {get; set;}
    public List<Contact> contactList {get;set;}
    public List<cWrapper> cListWrapper {get;set;}
    
    public wrapperClassController(){
        contactList = [SELECT Id, Name, Phone,Email FROM Contact WHERE AccountId=:recordId and email != null limit 10 ];
        cListWrapper = new list<cWrapper>();
        
        for(Contact c: contactList) {
            cListWrapper.add(new cWrapper(c));
        }
    }    
    public class cWrapper {
        
        public Contact  con {get; set;}
        public Boolean selected {get; set;}
        
        public cWrapper(Contact  c) {
            con = c;
            selected = false;
        }
    }
    
    public PageReference reset() {
        PageReference pg = new PageReference(System.currentPageReference().getURL());
        pg.setRedirect(true);
        return pg;
    }
    
    public PageReference sendEmails(){
        List<Contact> selectedContacts = new List<Contact>();
        //System.debug('==>Inside sendEmail');
        //system.debug('cListWrapper ==> ' + cListWrapper);
        for(cWrapper c: cListWrapper) {
            system.debug('c ==> ' + c );
            if(c.selected == true) {
                system.debug('Selected C  ==> ' + c );
                selectedContacts.add(c.con);
            }
        }
        System.debug('selectedContacts ==>'+selectedContacts);
        String []toAddresses = new List<String>();
        //String[] toAddresses = new String[] {};
        
        for(Contact c : selectedContacts) {
            toAddresses.add(c.Email);
        }
        System.debug('toaddresses==>'+toAddresses);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toAddresses);
        mail.setSubject('VF page created' );
        mail.setHtmlBody('This SF message!!!!!!   Thank you');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        //update selectedContacts;
        return null;
    }  
    
}

Thanks,
Maharajan.C