You need to sign in to do that
Don't have an account?
Akshu
I need help to solve this error
When click on send mail button:
*************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;
}
}
*************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;
}
}
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
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:
Class:
Thanks,
Maharajan.C