You need to sign in to do that
Don't have an account?

Send Email button not working
I have created a junction object 'SR Contacts' which has a M-D relationship with Contacts and a custom object 'SR'. I have created a 'send email' button to send an email to multiple contacts. The pop up window to select a template works but when I hit the SEND button nothing happens. (this is in Sandbox)
I'm not quite sure whether I'm using the correct contact API field - do I use the one on the junction object or the standard contact field? I just don't know whether the Contact_Name__c is the correct API but I took it from the custom object lookup fields to Contacts? I'm not even sure whether that's the problem! Anyway here is the code - thank you. Katerina
{!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')}
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.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 urecordIds = {!GETRECORDIDS( $ObjectType.Sales_Resource_Contacts__c)};
var contactIds = [];
var queryResult = sforce.connection.query('Select Name,Id, Contact__c from Sales_Resource_Contacts__c where Id in (\'' + urecordIds.join('\',\'') + '\')' );
var records = queryResult.getArray("records");
for (var a=0; a<records.length; a++) {
contactIds.push(records[a].Contact_Name__c);
}
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();
I'm not quite sure whether I'm using the correct contact API field - do I use the one on the junction object or the standard contact field? I just don't know whether the Contact_Name__c is the correct API but I took it from the custom object lookup fields to Contacts? I'm not even sure whether that's the problem! Anyway here is the code - thank you. Katerina
{!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')}
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.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 urecordIds = {!GETRECORDIDS( $ObjectType.Sales_Resource_Contacts__c)};
var contactIds = [];
var queryResult = sforce.connection.query('Select Name,Id, Contact__c from Sales_Resource_Contacts__c where Id in (\'' + urecordIds.join('\',\'') + '\')' );
var records = queryResult.getArray("records");
for (var a=0; a<records.length; a++) {
contactIds.push(records[a].Contact_Name__c);
}
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();
NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.
All Answers
NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.