Well Recently I was figuring it out to send email to a custom object email field through the list view page custom button. On click of the button it should open a small panel to select the Email Template and send it to the selected email.
Hmm pretty cool panel. But how to do it?
This is how I started,
This is how I started,
1) Create a global class as follows. (Note :- You need to replace the "Person_Detail__c" object with your custom object and the "Email__c" field with your custom email field)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global class SendEmailHandler { | |
webservice static boolean sendEmail(List<Id> personIds,string templateId) | |
{ | |
//Get all the Persons record where Id in personIds and email not equal to null | |
List<Person_Details__c> lstperson = [Select Id, Name, email__c from Person_Details__c where Id in :personIds and email__c != null]; | |
//Get the Contact Id to set it for setTargetObjectId of SingleEmailMessage | |
List<Contact> lstContact = [Select Id from Contact limit 1]; | |
Messaging.SingleEmailMessage[] emailMessageList = new Messaging.SingleEmailMessage[]{}; | |
//Loop through the Person record and Fill the EmailMessage List | |
for(Person_Details__c p : lstperson) | |
{ | |
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); | |
List<String> toaddress = new List<String>(); | |
toaddress.add(p.Email__c); | |
mail.setToAddresses(toaddress); | |
mail.setSenderDisplayName('MyCompanyName'); | |
mail.setTemplateID(templateId); | |
mail.setTargetObjectId(lstContact[0].Id); | |
mail.saveAsActivity = false; | |
mail.setWhatId(p.Id); | |
emailMessageList.add(mail); | |
} | |
//send the email with appropriate template to the person | |
Messaging.sendEmail(emailMessageList); | |
return true; | |
} | |
} |
2) Created a List View button named "Send Email" on your custom object with content source as "onclick Javascript" option
3) Below is the Javascript code for the button. (Note:- In my case I used "Person_Detail__c" as custom object. You need to use your custom object instead of "Person_Detail__c")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} | |
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")} | |
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')} | |
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js')} | |
function appendTags(){ | |
if(jQuery('[id=start-theme-css]').length==0){ | |
jQuery('head').append( | |
'<link ' + | |
'id="start-theme-css"' + | |
'rel="stylesheet"' + | |
'href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/start/jquery-ui.min.css"' + | |
'type="text/css" />' | |
); | |
jQuery('body').append( | |
'<div id="dialog-confirm" title="Send Mass Email">' + | |
'<p style="text-align:justify">' + | |
'<img src="/s.gif" alt="Contact" class="pageTitleIcon" title="Contact" style="margin: 0 7px 30px 0"/>' + | |
'Please select an email template to use.' + | |
'<br/><br/>Email Template:<br/>' + | |
'<select id="email-template" style="width:380px"></select>' + | |
'</p>' + | |
'</div>' | |
); | |
} | |
} | |
function createPopupWindow(){ | |
jQuery(function() { | |
jQuery( "#dialog-confirm" ).dialog({ | |
resizable: false, | |
width: 400, | |
modal: true, | |
show: { | |
effect: "fade", | |
duration: 500 | |
}, | |
hide: { | |
effect: "fade", | |
duration: 500 | |
}, | |
buttons: { | |
"Send Mail": | |
function() { | |
sendMail(); | |
}, | |
Cancel: | |
function() { | |
jQuery( this ).dialog( "close" ); | |
} | |
} | |
}); | |
}); | |
} | |
function fetchEmailTemplates(){ | |
var emailTemplates = | |
sforce.connection.query( | |
'SELECT Id, Name FROM EmailTemplate', | |
{ | |
onSuccess: | |
function(result){ | |
var records = result.getArray('records'); | |
var innerHtml = '<option value="">--Select--</option>'; | |
for(var i=0; i<records.length; i++) | |
innerHtml += | |
'<option value="' + records[i].Id + '">' + | |
records[i].Name + | |
'</option>'; | |
jQuery('[id=email-template]').html(innerHtml); | |
}, | |
onFailure: | |
function(error){ | |
alert('An Error has Occurred. Error: ' + error); | |
} | |
} | |
); | |
} | |
function sendMail(){ | |
var perIds = {!GETRECORDIDS( $ObjectType.Person_Details__c)}; | |
var templId = jQuery('[id=email-template]').val(); | |
var flag = sforce.apex.execute("SendEmailHandler", "sendEmail", {personIds:perIds,templateId:templId}); | |
if(flag) | |
{ | |
jQuery("#dialog-confirm").dialog( "close" ); | |
alert('Your emails have been sent successfully'); | |
} | |
} | |
appendTags(); | |
fetchEmailTemplates(); | |
createPopupWindow(); |
That's it, now click on the "Send Email" button and send cool template emails to your recepients..