function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Erich.WeihrauchErich.Weihrauch 

Using an S-Control to send a prescribed email to a prescribed user?

Would it be possible to use the s-control to create a button that when clicked, automatically sends an email to a prescribed user?  This control would also ideally be placed within a case, and when pressed, it would send the prescribed user the initial case details (e.g. initial case description).  Is this possible? How do I do this? I'm a bit new to scripting with S-Controls and such and the demos here on Salesforce didn't quite do it for me.

Thanks!

Greg HGreg H
This is possible and difficult to explain in a single post. At a high level I would create a button on the page layout and link that button to a custom sControl.  The sControl would reside inside the frame of the page and basically look like the "Send an Email" task screen except not allow for other template selection and actually illustrate the message that will be sent to the prescribed address you mention. On this screen they would be able to confirm that they want to actually send the message. Upon confirmation you could use the API to send your email and then redirect the user back to the case detail page.
 
To make this thing really useful I would suggest using an inline sControl instead of a button on your case detail page and in the sControl I would mimic a button.  Then in that code I would only display the button if the message you are prompting for was not yet sent.
 
I don't know of a document or a demo that actually illustrates this but I'm sure you could get someone to build it for you if you posted this on the jobs board or something.
-greg
Erich.WeihrauchErich.Weihrauch
Here is what I have so far..but it's throwing a syntax error...

<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
<script>
var user = sforce.connection.getUserInfo().userEmail;


//called to send email
var request = new sforce.SingleEmailMessage();

//create variable to hold addresses

var addr = new sforce.StringBuffer();

var subj = new sforce.StringBuffer();

var body = new sforce.StringBuffer();



//assign vaiables

addr = "me@here.com"

subj = "Important Notice: Case Out of the Ordinary Test";

body = "Testing";

//get variables
addr.append = addr;

request.replyTo = user;

request.subject = subj;

var sendMailRes = sforce.connection.sendEmail([request]);


</script>

While I'm somewhat familiar with Javascripting, I'm a little lost with the Sforce stuff..


Message Edited by Erich.Weihrauch on 02-14-2008 10:39 AM
Greg HGreg H
I recommend reading up on the API documentation because that will show all of the capabilities of the API with regards to sending email.  For some additional pointers I put together a function that will send an HTML email to a contact. I tried to comment as much of the code as possible to make it easier to understand.
Code:
 var subjectHTML = "Sample Subject Line";
 //create the header portion of the HTML message
 var emailBody = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
 emailBody += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
 emailBody += "<head>";
 emailBody += "<title>Sample HTML Message</title>";
 emailBody += "</head>";
 emailBody += "<body>";
 emailBody += "<p>Hello,</p>";
 emailBody += "<p>This is a test message...</p>";
 emailBody += "<p>Thanks for reading.</p>";
 emailBody += "<p>Written by,<br>Greg H</p>";
 emailBody += "<div>greg[at]InteractiveTies[dot]com</div>";
 emailBody += "<div><a href=\"http://www.interactiveties.com/\" target=\"_blank\">www.InteractiveTies.com</a></div>";
 emailBody += "</body>";
 emailBody += "</html>";
 //text version in case HTML is not accepted by receiver
 var textBody = "Hello,\r\n";
 textBody += "This is a test message...\r\n";
 textBody += "Thanks for reading.\r\n";
 textBody += "Written By,\r\n";
 textBody += "Greg H\r\n";
 textBody += "---------------------------------------\r\n";
 textBody += "greg[at]InteractiveTies[dot]com\r\n";
 textBody += "www.InteractiveTies.com\r\n";
 var request = new sforce.SingleEmailMessage(); //instantiate the send message capabilities of AJAX Toolkit
 //var sendTo = new Array("first1.last1@example.com","first2.last2@example.com","first3.last3@example.com"); //additional to email addresses (if necessary)
 var sendBCC = new Array("first1.last1@yourCompany.com","first2.last2@yourCompany.com","first3.last3@yourCompany.com"); //array of blind carbon copy addresses (if necessary)
 request.useSignature = false; //flag to indicate use of salesforce default signature
 request.emailPriority = "Normal"; //message priority
 request.saveAsActivity = true; //flag to indicate logging of activity in contact history
 //request.toAddresses = sendTo; //assignment of to address array (if necessary)
 //request.ccAddresses = sendCC; //assignment of cc address array (if necessary)
 request.bccAddresses = sendBCC; //assignment of bcc address array (if necessary)
 request.subject = subjectHTML; //assignment of subject
 request.htmlBody = emailBody; //assignment of message body
 request.plainTextBody = textBody; //assignment of message body in plain text
 //var attachments = new Array("000000000000000"); //array holding references to all files needing to be attached to message (if necessary)
 //request.documentAttachments = attachments; //associate the salesforce document from above (if necessary)
 request.targetObjectId = "000000000000000"; //associated salesforce.com contact id
 request.whatId = "000000000000000"; //associated salesforce.com what id (if necessary)
 request.replyTo = "support@yourCompany.com"; //reply to address
 request.senderDisplayName = "Your Company - Support"; //From Display
 var sendMailRes = sforce.connection.sendEmail([request], layoutResults); //actual request to send message (upon completion run layoutResults function)

Also, I was able to find a sample that may be able to assist you in more detail.  The example they wrote uses a custom interface but ultimately sends an email using the API.  Here is the sample: http://wiki.apexdevnet.com/index.php/Send_Emails_Through_SControls
-greg
Erich.WeihrauchErich.Weihrauch
Hey, thank you for that bit of code.

I was actually working from that link you gave me, but guess I didn't quite get things right.
I've been trying to read up on the documentation but I guess being more of a non-coder...well I do php and mysql but not a lot of javascript and such..I'm having a tough time wrapping my head around it.

Also, what I don't quite understand, based off of what I read, is how do I implement the code?  When developing a custom s-control what kind is it? URL, snippet, etc?  When using that as a button in a case details layout I want to be able to just click a button and have the code executed and am having a tough time doing that as well.
Greg HGreg H
I'm glad you found that code useful.  I work with PHP and mySQL too so let me know when you encounter issues with PHP and the Salesforce API/SOAP calls - because that took me a while to work through and I could probably save you some time.
 
For the purposes of the code I've been posting here the sControl being built will be HTML.  I actually find that I use the HTML type more than the URL or Snippet types.
 
As for calling the code you can use a couple of different methods including a custom button or link (Setup > App Setup > Customize > [Object] > Buttons and Links), or you could make a formula fied on the object to create a custom link directly to the sControl (Setup > App Setup > Customize > [Object] > Fields), or you could do something else entirely.  It really depends on the functionality you want to see from the Users' standpoint and I recommend you try a number of ways to make sure it performs as desired.
 
If you use a custom formula field on the object then make sure you call the sControl directly using a click thru like this:
Code:
/servlet/servlet.Integration?lid=000000000000000&ic=1

Where the "000000000000000" portion of the click is the Id of your HTML sControl you created.
-greg
Erich.WeihrauchErich.Weihrauch
I think maybe i'm still missing the boat somewhere...
I've taken your code, and popped it into an HTML s-control, saved it, then went to the case section for customization, created a detail page button, linked it to an s-control and behavior is pop up in new window...

When I then click the button in a case, it pops up a window and all I see is..

<script type="text/javascript">function getRecordIds(keyPrefix) { return [] }</script><script type="text/javascript">__sfdcSessionId = '460700D700000008Bbn!ARQAQLtFKsPfCM4iQsDz0DFaS_y1.uA2YOopdsNNb2Rw.zIJVnokYqIX_lDGmWJgerwFg4FKEw.aIximDMXHXgx9cPMgCFFs'</script>var subjectHTML = "Test"; //create the header portion of the HTML message var emailBody = ""; emailBody += ""; emailBody += " "; emailBody += ""; emailBody += ""; emailBody += ""; emailBody += "

Hello,

"; emailBody += "

This is a test message...

"; emailBody += "

Thanks for reading.

"; emailBody += "

Written by,
Greg H

"; emailBody += "
greg[at]InteractiveTies[dot]com
"; emailBody += ""; emailBody += ""; emailBody += ""; //text version in case HTML is not accepted by receiver var textBody = "Hello,\r\n"; textBody += "This is a test message...\r\n"; textBody += "Thanks for reading.\r\n"; textBody += "Written By,\r\n"; textBody += "Greg H\r\n"; textBody += "---------------------------------------\r\n"; textBody += "greg[at]InteractiveTies[dot]com\r\n"; textBody += "www.InteractiveTies.com\r\n"; var request = new sforce.SingleEmailMessage(); //instantiate the send message capabilities of AJAX Toolkit //var sendTo = new Array("erich.weihrauch@assuretec.com","first1.last1@yourCompany.com","first2.last2@yourCompany.com","first3.last3@yourCompany.com"); //additional to email addresses (if necessary) var sendBCC = new Array("first1.last1@yourCompany.com","first2.last2@yourCompany.com","first3.last3@yourCompany.com"); //array of blind carbon copy addresses (if necessary) request.useSignature = false; //flag to indicate use of salesforce default signature request.emailPriority = "Normal"; //message priority request.saveAsActivity = true; //flag to indicate logging of activity in contact history //request.toAddresses = sendTo; //assignment of to address array (if necessary) //request.ccAddresses = sendCC; //assignment of cc address array (if necessary) request.bccAddresses = sendBCC; //assignment of bcc address array (if necessary) request.subject = subjectHTML; //assignment of subject request.htmlBody = emailBody; //assignment of message body request.plainTextBody = textBody; //assignment of message body in plain text //var attachments = new Array("000000000000000"); //array holding references to all files needing to be attached to message (if necessary) //request.documentAttachments = attachments; //associate the salesforce document from above (if necessary) request.targetObjectId = "000000000000000"; //associated salesforce.com contact id request.whatId = "000000000000000"; //associated salesforce.com what id (if necessary) request.replyTo = "support@yourCompany.com"; //reply to address request.senderDisplayName = "Your Company - Support"; //From Display var sendMailRes = sforce.connection.sendEmail([request], layoutResults); //actual request to send message (upon completion run layoutResults function)

I'm not sure what I'm doing wrong..do I need to wrap the code in html or declare it as a javascript?
Greg HGreg H
You should wrap it all in HTML. The function I wrote was actually only a portion of the full code you'll need. The script is simply the part that does the emailing.
-greg
hemantgarghemantgarg

How to attach a file with email while using AJAX API ? can you post that piece of code plz ?

sugansugan

Does  this work for file attachments as well? as the code given seems to work fine as long as it is documents but when the Attachment id is used there is no attachment in the mail.