+ Start a Discussion
GlennAtAppirioGlennAtAppirio 

Sending A Template-Based Email From an S-Control

These boards have numerous threads about sending emails from s-controls.  Having just wrestled with a variant of the problem for a customer, I thought I'd post the solution I used, which (now!) seems fairly straightforward (although it took some bobbing & weaving to get here).

Problem Statement
:  With one click, allow a user to create an object, then send a template-based email to a Contact associated with that object.  The email template must populate with correct values from the just-created object.

Solution:  Save the following code as an s-control snippet.  Then include the snippet, and call the function from anywhere in your main s-control.  (I won't cover the part about creating a new object here - let's assume you can do that, and then retrieve the object ID of your newly created object.)

function sendEmail(templateId, contactIdForToAddress, relatedObjectId) {

  var url = "https://na3.salesforce.com/email/author/emailauthor.jsp?";
  url += "p2_lkid=" + contactIdForToAddress;
  url += "&p3_lkid=" + relatedObjectId;
  url += "&template_id=" + templateId;
  url += "&new_template=1";
  url += "&retURL=%2F" + relatedObjectId;
  url += "&nosave=0";   // not sure what this parameter does, or if it's really needed
  url += "&save=1";   // send it now - don't just bring us to the "send email" page

  window.open(url);
}


The net effect here is that a new window (actually, a tab in Firefox) will open up, pointing at the newly created object.  If it has an Activity History related list, the just-sent email will appear there. 

This all worked rather elegantly for me.  The actual use case is a Google Map showing the location of various Communities (a custom object).  The user can select several Communities (from a sidebar with a list of accounts & checkboxes), then click a single button to create "Referrals" (a custom object) to each selected Community, and email each Community's primary contact with their Referral data.  After the click, the user waits a moment, and then multiple tabs open up, one for each newly-created Referral.

I'm happy to discuss further if anyone is interested or needs help doing something similar - glenn@appirio.com.


Message Edited by GlennAtAppirio on 02-18-2007 01:05 PM

Message Edited by GlennAtAppirio on 02-18-2007 01:05 PM

Best Answer chosen by Admin (Salesforce Developers) 
wickwick
I have a custom button to create a new custom object.
When the user saves the new custom object, I want a new email to be composed (but not sent yet) using a template, and related to the custom object that was just saved. I also want to pull in the contact field from that custom object, as well as up to 5 email addresses from 5 email address fields in the custom object.
It seems that I need to add some procedure to the "save url" part of my original custom button URL, but when I do that it doesn't use the template or pull in any custom fields. Could I run an S-control from the "Save URL" parameter of my URL to do this? If so, how do I run an S-control from the SaveURL part of a URL, and what would my S-Control need to contain.
I appreciate any advice, code, or links to other posts to help me tackle this.
Thank you.

All Answers

JT LovellJT Lovell

Here's a slight variation of what Glenn has posted.  In this case, I have a button s-control that launches a jump screen where the user fills out a little form that determines and looks up who the email should be going to including some additional to and cc addresses.  Then it calls the email form and leaves them looking at the edit screen rather than firing the email silently.  The idea is that the user can add some additional verbiage before the final send if they wish to.  Here's the send email function:

 

Code:

function sendEmail(templateId, contactIdForToAddress, relatedObjectId, toAddresses, ccAddresses, bccAddresses) {

  var url = "/email/author/emailauthor.jsp—";
  url += "p2_lkid=" + contactIdForToAddress;
  url += "&p3_lkid=" + relatedObjectId;
  url += "&p24=" + toAddresses; // additional to
  url += "&p4=" + ccAddresses;
  url += "&p5=" + bccAddresses;
  url += "&template_id=" + templateId;
  url += "&new_template=1";
  url += "&retURL=%2F" + relatedObjectId;
  // commented these lines to drop the user on the email edit screen rather than sending silently
  // url += "&nosave=0";
  // url += "&save=1";

  window.parent.location.href = url;
}


 
JT Lovell
jt_at_appirio.com

arasuarasu
Hi,
 
I have the following questions regarding your suggestion:
 
1. In your function sendEmail(templateId, contactIdForToAddress, relatedObjectId) , can we pass some dynamic variables that need to be used in the template? For. e.g: I want to pass the Account Name, Account Owner, etc and some other related Object details.
2. Can the contactIdForToAddress parameter be from USERS object or can it be only be from CONTACTS? The requirement that we have is to send emails to user emailids from the USERS object.
 
Appreciate if you could please throw some light on the above. Basically our requirement is to send emails dynamically from S-control if user declines an already approved product. The emails need to  be sent to the relevant product owners. The functionality to approve product is already in an S-control. So the send email function also has to be in the S-Control.
 
Thanks and regards,
Ambili
JT LovellJT Lovell
>>1. In your function sendEmail(templateId, contactIdForToAddress, relatedObjectId) , can we pass some dynamic variables that need to be used in the template? For. e.g: I want to pass the Account Name, Account Owner, etc and some other related Object details.
 
Remember that you're using an email template and that supports merge fields.  You can include account merge tokens such as the account name and ID in the template and then pass the AccountId as the relatedObjectId and Salesforce will fill the tokens when it builds the page.
 
>>2. Can the contactIdForToAddress parameter be from USERS object or can it be only be from CONTACTS? The requirement that we have is to send emails to user emailids from the USERS object.
 
I don't believe user emails are supported directly using this model, but your s-control could lookup the user email addresses, and then include them in the p24 parameter, which is the "additional to" field which can contain additional email addresses.  If no contact is involved, just leave the ContactId blank or omit the parameter entirely.  Look at the 2nd version of the function posted on 5-1-2007 for details on how to use the additional parameters.
 
JT
jt/at/appirio/dot/com

Message Edited by JT Lovell on 10-26-2007 01:49 PM

Rasmus MenckeRasmus Mencke

You should use the Send Email API instead of calling the URL. Sample code is available here

http://wiki.apexdevnet.com/index.php/Send_Emails_Through_SControls

arasuarasu

Hi Rasmus,

Thanks a lot for your response! This is exactly what I have been hunting... for sending dynamic emails from S-control without having to rely on templates. I tried it and it works perfectly.

Thanks again for your timely help.

Regards,
Ambili

WhyserWhyser
Is there any updates in regards to sending an email through S-Controls?
 
I was wondering if it would be possible to use Letterheads in our templates and have the contents written into the letterhead dynamically.
Rajesh ShahRajesh Shah
If I need to attach a file with the email, what changes in the above would be required?
marie.tournemarie.tourne
Have you find a way to pass additionnal merge fields to standard email templates ?
For example related objects fields ?

Thanks in advance
Rajesh ShahRajesh Shah
Unfornuately no. :smileysad:
wickwick
I have a custom button to create a new custom object.
When the user saves the new custom object, I want a new email to be composed (but not sent yet) using a template, and related to the custom object that was just saved. I also want to pull in the contact field from that custom object, as well as up to 5 email addresses from 5 email address fields in the custom object.
It seems that I need to add some procedure to the "save url" part of my original custom button URL, but when I do that it doesn't use the template or pull in any custom fields. Could I run an S-control from the "Save URL" parameter of my URL to do this? If so, how do I run an S-control from the SaveURL part of a URL, and what would my S-Control need to contain.
I appreciate any advice, code, or links to other posts to help me tackle this.
Thank you.
This was selected as the best answer
rpr2rpr2
MTO - Looks like this is coming in the Winter '09 release with Visualforce templates
marie.tournemarie.tourne
Thanks for the tip, I am looking forward to it then !

Before Winter 09, here is the technique I used :
- Create email template in Salesforce from word document (with copy/paste)
- Take the HTML code generated to inject it in emailauthor.jsp
- Delete email template (I use it only to help the html code generation)

The HTML code is not very clean but it works well and it's quick.


PARTHA KARMAKARPARTHA KARMAKAR
I want to create website like https://dailygovt.com/ (https://dailygovt.com" target="_parent)