Last active
June 30, 2017 19:39
-
-
Save alexkadis/15a45cc44f1712b18dfebda50401921a to your computer and use it in GitHub Desktop.
Changed Related ID to correctly use Campaign ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////// | |
// Title: Salesforce Send Mass Email from Campaign Members (uses built-in email) | |
// 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.CampaignMember)}; | |
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 = "{!Campaign.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 Campaign Member record."); | |
} | |
else { | |
var result = sforce.connection.query('SELECT Contact.Name, Contact.Id, Contact.Email FROM CampaignMember WHERE Id IN ' +csvContactIds); | |
var records = result.getArray("records"); | |
var email = ""; | |
for (var i=0; i< records.length; i++) { | |
email += records[i].Contact.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; | |
} | |
// create new contact with certain details | |
// https://developer.salesforce.com/forums/?id=906F00000008jRMIAY | |
// Email affiliated contacts | |
// https://gist.github.com/mdhorine/929c3fc48ef62b566b0e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment