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
Josh HarshJosh Harsh 

Quick Mass Email on Custom Object Related list

I stumbled accross the below formula while scanning through the community. It has worked very well for us but due to a change in the system we need to have it slightly changed.

{!REQUIRESCRIPT('/soap/ajax/28.0/connection.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="Quick Mass Mailer">' +
'<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. To create a new template, you must exit this mass email process and create the new template in your personal setup section.' +
'<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: "bounce",
duration: 500
},
hide: {
effect: "bounce",
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 contactIds = {!GETRECORDIDS( $ObjectType.Contact )};
var templateId = jQuery('[id=email-template]').val();
if(contactIds.length>0 && templateId!=''){
var massMailRequest = new sforce.MassEmailMessage();
massMailRequest.targetObjectIds = contactIds;
massMailRequest.templateId = templateId;
massMailRequest.replyTo = 'noreply@salesforce.com';

sforce.connection.sendEmail([massMailRequest]);

alert('Your emails have been submitted for processing.');
}
}

appendTags();
fetchEmailTemplates();
createPopupWindow();


We are going to be linking the contact via look up to a custom object in salesforce and we will then have the custom object related list on the record. We would like  to be able to send the contacts a mass emailer through the custom object. I have tried to manipulate the code to allow this but cannot seem to figurre it out. Would anyone be able to assist?