+ Start a Discussion

Problem with sending email using S-Controls?



I had created a S-Controls to send  emails through S-Controls based on salesforce.com tutorial. Here i can able to send emails, but i want to add the emails by selecting the records. I had created a custom list button and added in List Pagelayout, here users can select the records and when clicked on Email button it will navigate to S-Control page and in that page i'm not able to get the toAddress of the selected records. For my reference i had given the alert box to verify whether i'm able to get the selected records or not. But alert box displaying all the records i hav chosen. Can any post how to solve this problem.




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script> <script> function startPage() { IdArray = {!GETRECORDIDS( $ObjectType.Student__c )}; alert("The Selected Ids are: "+IdArray); for(var i=0; i<IdArray.length; i++) { alert(IdArray[i]); } for(var j=0; j<IdArray.length; j++) { var selectedId = sforce.connection.query( "select Email__c from Student__c where Id ='"+IdArray[j]+"' "); alert("Selected :"+ selectedId); var records = selectedId.getArray('records'); for (var k=0; k< records.length; k++) { var record = records[k]; //alert("Record Size :"+ records.length ); alert("Records: "+record.Email__c); } } var user = sforce.connection.getUserInfo().userEmail; document.getElementById('reply_to').value = user; } function setupPage() { // Called to send email var request = new sforce.SingleEmailMessage(); // create variable to hold addresses var addr= new sforce.StringBuffer(); //addr = record.Email__c; //var addr = record.Email__c; var cc= new sforce.StringBuffer(); var bcc= new sforce.StringBuffer(); addr.append(document.getElementById("to_address").value); cc.append(document.getElementById("cc_to").value); bcc.append(document.getElementById("bcc_to").value); if(document.getElementById("useSignature").checked) {request.useSignature = true; } else { request.useSignature = false; } // Email Priority is being set based on what the user selects request.emailPriority = document.getElementById("emailPriority").value; // Reply to email address of the email being set request.replyTo = document.getElementById("reply_to").value; if (document.getElementById("email_subject").value!="") { request.subject = document.getElementById("email_subject").value; } else { alert("your subject is empty, please enter a subject line or it will appear blank"); } if (document.getElementById("contentText").checked) { //request.plainTextBody=document.getElementById("email_body").value; } request.htmlBody=document.getElementById("email_body").value; } if (document.getElementById("contentHtml").checked) { request.htmlBody=document.getElementById("email_body").value; } var ToArray = new Array; ToArray = addr.toString().split(","); var CcArray = new Array; CcArray = cc.toString().split(","); var BccArray = new Array; BccArray = bcc.toString().split(","); request.toAddresses = ToArray; if (document.getElementById("cc_to").value != "") {request.ccAddresses = CcArray; } if (document.getElementById("bcc_to").value != "") {request.bccAddresses = BccArray; } var sendMailRes = sforce.connection.sendEmail([request], layoutResults); } function layoutResults(sendEmailResult) { var textNode; if (sendEmailResult.length > 0) { if (sendEmailResult[0].getBoolean("success")) { document.getElementById("output").innerHTML = "Email has been sent!";} else { document.getElementById("output").innerHTML = sendEmailResult[0].errors ;}} else { document.getElementById("output").innerHTML = "Didn't get return data."; } } </script> <style type="text/css"> <!-- .error { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; background-color: A2C2EE; } .style12 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; } .style6 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; height: 16px; width: 400px;} .style7 {font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px; height: 150px;width: 400px;} .style8 {font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;} .button {font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10px;font-style: italic;font-weight: normal; background-color: C3DAF9;height: 29px; width: 53px;} --> </style> </head> <body onLoad="startPage()"> <table width="600" border="0" cellspacing="1" cellpadding="1"> <tr> <td><div id="output" class="error"></div> </td> </tr> </table> <form name="send_email" method="post" action="javascript:setupPage()"> <table width="600" border="0" cellpadding="2" cellspacing="2" bgcolor="#C3DAF9"> <tr > <td align="right" height="50" valign="middle"><input name="Send" type="submit" class="button" id="Send" value="Send!"></td> <td align="left" height="50" valign="middle" > </td> </tr> <tr> <td width="100"><div align="right"><span class="style12">To:</span></div></td> <td width="486"><span class="style6"> <label> <input name="to" type="text" class="style6" id="to_address"> </label> </span></td> </tr> <tr> <td><div align="right"><span class="style12">cc:</span></div></td> <td><span class="style6"> <input name="cc_to" id="cc_to" type="text" class="style6"> </span></td> </tr> <tr> <td class="style12"><div align="right">Bcc</div></td> <td><span class="style6"> <input name="bcc_to" id="bcc_to" type="text" class="style6"> </span></td> </tr> <tr> <td><div align="right"><span class="style12">Reply-to:</span></div></td> <td><span class="style6"> <input name="reply_to" type="text" class="style6" id="reply_to"> </span></td> </tr> <tr> <td class="style12"><div align="right">Importance</div></td> <td><select name="emailPriority" id="emailPriority" class="style12"> <option value="Highest">Highest</option> <option value="High">High</option> <option value="Normal" selected="selected">Normal</option> <option value="Low">Low</option> <option value="Lowest">Lowest</option> </select> </td> </tr> <tr> <td><div align="right"><span class="style12">Subject</span></div></td> <td><span class="style6"> <input name="subject" type="text" class="style6" id="email_subject"> </span></td> </tr> <tr> <td><div align="right"></div></td> <td class="style12"> <input name="contentType" type="radio" id="contentText" value="text" checked>Text <input name="contentType" type="radio" id="contentHtml" value="html">HTML </td> </tr> <tr> <td><div align="right"><span class="style12">Body (text) </span></div></td> <td><span class="style6"> <textarea name="email_body" cols="20" rows="15" class="style7" id="email_body"></textarea> </span></td> </tr> <tr> <td><div align="right"><span class="style12">Use Signature </span></div></td> <td><input name="useSignature" type="checkbox" class="style8" id="useSignature" value="checkbox"></td> </tr> </table> </form> </body> </html>





Have you considered doing this with Apex and Visualforce Email templates? There's some complete code examples here that may help.