Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexkadis/5d11aa4792f8ed718fd53131f87587d2 to your computer and use it in GitHub Desktop.
Save alexkadis/5d11aa4792f8ed718fd53131f87587d2 to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////////////////////
// Title: Send email to contacts from a list view (no templates needed).
// Description:
// Allows for mass Salesforce emails without templates, with simply a click.
// Note that you could easily go over Salesforce's limits if you're not careful.
//
// By Alex Kadis, based on sources listed below, especially
// Matt Horine's "Email Affiliated Contacts button for use with
// Salesforce NPSP"
//
// Requires Salesforce NPSP
//
// Directions:
// Setup > Create > Objects > Affiliation > New Button or Link
// Display Type: List Button
// Behavior: Execute JavaScript
// Content Source: OnClick JavaScript
// Paste in code below
// Setup > Accounts > Page Layout > Edit your page layout
// Scroll down to Affiliated Contacts > Properties (wrench icon)
// > Buttons > Add your newly available button
//
// Sources:
// https://developer.salesforce.com/forums/?id=906F00000008rTJIAY
// https://success.salesforce.com/answers?id=90630000000h3K8AAI
// https://developer.salesforce.com/forums/?id=906F00000009BDSIA2
// http://501partners.com/salesforce-tip-email-many-contacts-at-once/
// https://gist.github.com/mdhorine/929c3fc48ef62b566b0e
//
// Similarly useful: create new contact with certain details
// https://developer.salesforce.com/forums/?id=906F00000008jRMIAY
////////////////////////////////////////////////////////////////
{!RequireScript("/soap/ajax/20.0/connection.js")}
var contactRecords= {!GETRECORDIDS($ObjectType.Contact)};
var csvContactIds = "";
//fetch csv format Ids
for(var rowNum in contactRecords){
csvContactIds += "'"+ contactRecords[rowNum] + "',";
}
//remove last comma
csvContactIds = csvContactIds.slice(0, csvContactIds.length - 1);
//Enclose the ids in round brackets
if(csvContactIds.length > 1){
csvContactIds = "(" + csvContactIds + ")";
}
// Email parameters:
// template_id = id of email template you want to pre-populate
// email_type=visualforce (if the template that you're using is a VF email template)
// new_template=1 = required if you're using a VF email template that contains an attachment
// p2_lkid = id of the record for the To address (a Contact or Lead)
// rttype = the id prefix for the type of the To address (e.g., if p2_lkid refers to a Contact, use rttype=003) See: https://help.salesforce.com/articleView?id=Standard-Field-Record-ID-Prefix-Decoder&language=en_US&type=1
// p3_lkid = id of the record for the Related To (i.e., What) record (e.g., an Account or Opportunity)
// p4 = CC address
// p5 = BCC address
// p6 = Email subject
// p7 = Email body
// p24 = Additional To
// p26 = From address
// doc_id = the id of a Document or QuoteDocument to become an email attachment
// retURL = URL to return to after the email is sent
// cancelURL = URL to return to if the user clicks Cancel
var defaultTemplateID = " ";
var toId = " "; // cannot be a user
var toIdPrefix = "003";
var relatedID = "{!Account.Id} ";
var ccEmails = " ";
var additionalTo = "{!$User.Email} ";
var contactEmails = " ";
// return to current URL
var retURL = encodeURIComponent(window.location.href);
if (contactRecords.length ==0) {
alert("Please select at least one Contact record.");
}
else {
var result = sforce.connection.query("select Name, Id, Email from Contact Where Id IN " +csvContactIds);
var records = result.getArray("records");
var email = "";
for (var i=0; i< records.length; i++) {
email += records[i].Email + '; ';
}
contactEmails += email;
window.location.href= "/email/author/emailauthor.jsp?"
+"&template_id="+defaultTemplateID
+"&p2_lkid="+toId
+"&rtype="+toIdPrefix
+"&p3_lkid="+relatedID
+"&p4="+ccEmails
+"&p5="+contactEmails
+"&p24="+additionalTo
+"&retURL="+retURL
+"&cancelURL="+retURL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment