Skip to content

Instantly share code, notes, and snippets.

@mdhorine
Created January 28, 2015 13:50
Show Gist options
  • Save mdhorine/929c3fc48ef62b566b0e to your computer and use it in GitHub Desktop.
Save mdhorine/929c3fc48ef62b566b0e to your computer and use it in GitHub Desktop.
Email Affiliated Contacts button for use with Salesforce NPSP
/**************************************************
/ Button: Email Affiliated Contacts /
/ Author: Matt Horine /
/ Client: Step Up to Serve /
/ Date: 28 January 2015 /
/ /
/ Description: Button on Affiliated Contacts /
/ related list to allow selection and email of /
/ affiliated contacts from the Account Page in /
/ the same way the Send an Email button allows /
/ selection of contacts. /
**************************************************/
// Notes and To Do
// To Do: Add button to full list view
// Note: Shows in Account activity history but not contact
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}
// Get current url
var url = parent.location.href;
// Get the current Account Id
var acctId = "{!Account.Id}";
// Get the set of Affiliated Contacts that have been selected
var records = {!GETRECORDIDS($ObjectType.npe5__Affiliation__c)};
// Create string of Ids for use in the SOQL Query ('X','Y','Z')
var ids = '';
for (var i=0; i<records.length; i++) {
ids = ids + "'" + records[i] + "'";
if (i<records.length-1) {
ids = ids + ',';
}
}
// Create array to hold the list of emails
var emailList = [];
// If no records selected, alert user
if (records[0] == null) {
alert("Please select at least one Affiliated Contact to email.");
}
else {
// Create and run SOQL Query
var queryResult = sforce.connection.query("SELECT Id, npe5__Contact__c, npe5__Contact__r.Email from npe5__Affiliation__c where Id in (" + ids + ")");
// Place results in array variable
var emailArray = queryResult.getArray("records");
// Place email for each result in the email list array
for (var j=0; j<emailArray.length; j++) {
var cont = emailArray[j].npe5__Contact__r.Email;
emailList.push(cont);
}
// Create URL for the send an email page. p24 is the Additional To
// field. p4 is the CC field, and p5 is the BCC field.
parent.location.href = '/_ui/core/email/author/EmailAuthor?p3_lkid=' + acctId + '&retURL=%2F' + acctId + '&p24=' + emailList;
}
@alexkadis
Copy link

Thank you for this! I made some changes - available here if anyone wants.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment