Salesforce Tip: Email Many Contacts at Once

By August 3, 2015 January 4th, 2022 Data Management, Nonprofit Tech

Have you ever looked at a list of Salesforce contacts and wished you could email some of them without having to create a campaign or a special view?  Have you ever been frustrated that the NPSP’s Affiliated Contacts structure removes the one useful part of Salesforce emailing (the ability to find other contacts with the same Account)?  Do you often need to email all Contacts affiliated with an Account?

One of the enduring mysteries of Salesforce is why it isn’t easier to email groups of people.  Salesforce is, after all, a constituent relationship management tool, and for good or ill email is still our go-to for communications.  The short version of the story is that Salesforce isn’t a mass mailer, which has to comply with anti-spam laws and deal with resource constraints. But there are lots of things you can do to email more easily from Salesforce, including some good old fashioned button hacking to make life easier.

In this post, we’ll take you step-by-step through adding an “Email Selected Contacts” button to your Contact views, and adding the same button to your Affiliated Contacts related list if you’re using the Nonprofit Starter Pack (NPSP).

This post owes a huge debt to a couple of Google Group posts with particular thanks to Jon LaRosa for providing the bulk of the JavaScript required.  We’ve made some little tweaks to send you directly to Salesforce’s emailer, and restrict the number of people you can email at any one time.  Important note: this button has a restriction of 50 contacts selected!  There’s a limit to how many characters you can send to the emailer using this button method, and there’s a limit to how many people you can mail at once.

Part 1: Add Email Selected Contacts to Contacts View

First, create the button.

  1. Navigate to Setup->Customize->Contact->Buttons, Links and Actions
  2. Click “New Button or Link”
  3. Set up your Button Settings as follows:
    • Name: Email Selected Contacts
    • Display Type: List Button (check the box for Display Checkboxes for Multi-Record Selection)
    • Behavior: Execute Javascript
    • Content Source: Onclick Javascript
    • Paste the code in that is below this list – you should not need to make any changes
  4. Save

Then, add the button to your Contacts list views.

  1. Navigate to Setup->Customize->Contact->Search Layouts
  2. Edit “Contacts List View”
  3. Add the new button
  4. Test

Code to paste in for contact button:
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')}
var records = {!GETRECORDIDS($ObjectType.Contact)};
var recipients = [];
if (records[0] == null) {
alert("Please select at least one contact to email.");
}
else if(records.length > 50) {
alert("Please use a different mechanism to email more than 50 people at once.");
}
else {
// Iterate through records
for (var n=0; n < records.length; n++) {
// retrieve Contact based on the ID in records
var contactLookup = sforce.connection.retrieve("Name,Email","Contact",[records[n]]);
if(contactLookup[0].Email){
// email is not empty so add to list
recipients.push(contactLookup[0]);
}
}
// Build the string for a mass email
// Change the seperator between a semi-colon or comma, depending on email client
var toEmailString = "";
for (var n=0; n < recipients.length; n++) {
toEmailString = toEmailString + recipients[n].Email + ",";
}
// Remove last comma or semi-colon from string
toEmailString = toEmailString.slice(0,toEmailString.length-1);
// redirect to emailer
window.location='/_ui/core/email/author/EmailAuthor?p24='+toEmailString;
}

Part 2: Add Email Selected Contacts to Affiliated Contacts Related List (Accounts)

First, create the button.  Technical note: this button references the Affiliated Contacts object from the Nonprofit Starter Pack (specifically, version 3). For the more technically inclined, contrast this code to the above Contact View code to get a sense of how you could add this same type of interaction to any child object/related list that has email addresses.  Important Note: You do have to select the emails to send to,

  1. Navigate to Setup->Create->Objects->Affiliation->Buttons, Links and Actions
  2. Click “New Button or Link”
  3. Set up your Button Settings as follows:
  4. Name: Email Selected Contacts
  5. Display Type: List Button (check the box for Display Checkboxes for Multi-Record Selection)
  6. Behavior: Execute Javascript
  7. Content Source: Onclick Javascript
  8. Paste the code in that is below this list exactly (no changes at all)
  9. Save
  10. Add the new button to the Affiliated Contacts Related List on the Accounts page layout
  11. You should notice that checkboxes are now available on this related list!

Code to paste in for affiliated contacts related list button:
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')}
var records = {!GETRECORDIDS($ObjectType.npe5__Affiliation__c)};
var recipients = [];
if (records[0] == null) {
alert("Please select at least one contact to email.");
}
else if(records.length > 50) {
alert("Please use a different mechanism to email more than 50 people at once.");
}
else {
// Iterate through records
for (var n=0; n < records.length; n++) {
// retrieve Contact based on the ID in records
var contactLookup = sforce.connection.retrieve("npe5__Contact__r.FirstName,npe5__Contact__r.LastName,npe5__Contact__r.Email,npe5__Organization__r.Id,npe5__Organization__r.Name","npe5__Affiliation__c",[records[n]]);
if(contactLookup[0].npe5__Contact__r.Email){
// email is not empty so add to list
recipients.push(contactLookup[0]);
}
}
// Build the string for a mass email
// Change the seperator between a semi-colon or comma, depending on email client
var toEmailString = "";
for (var n=0; n < recipients.length; n++) {
toEmailString = toEmailString + recipients[n].npe5__Contact__r.Email + ",";
}
// Remove last comma or semi-colon from string
toEmailString = toEmailString.slice(0,toEmailString.length-1);
// redirect to emailer
window.open('/_ui/core/email/author/EmailAuthor?p24='+toEmailString+'&p3_lkid='+recipients[0].npe5__Organization__r.Id+'&p3='+recipients[0].npe5__Organization__r.Name);
}

Allan Huntley

Author Allan Huntley

More posts by Allan Huntley