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
zero1578zero1578 

Send Reply button from Tasks

We use Tasks to append emails into our cases so that they dont clutter the comments section.  Doing this also allows us to use the content for other things, such as sending replies with the original customer email included.
 
Heres the code, and it will need to be customized some but I had to go through and figure out how to handle the HTML/Text-Only switches in the Email page.  I also had to provide the team with a way to send the email and refresh their case showing that it was sent.
 
 
I dont know if this will be useful to anyone, but our team has found it pretty useful so I figured I would share it.  It can probably be optimized a lot, thats not one of my strong points.
 
Code:
<script type="text/javascript" language="javascript" src="/soap/ajax/9.0/connection.js"></script> 
<script type="text/javascript" language="javascript" src="/soap/ajax/9.0/apex.js"></script> 


<script language="JavaScript"> 
//the textarea in the HTML stores the actual "Content" 
//of the body from the Task page to be passed to the email page



//boolean to store a value so when you switch mail content type
//from HTML to Text-Only it doesnt re-write the entire body with
//original content only, erasing your recent additions.

var hasBeenUsed = false; 
function getContents() 
{ 
if(!hasBeenUsed) 
{

//get which text mode you are in, html or text only 
var checkHTML = window.frames['myEmail'].document.getElementById('iframe_p23'); 
var checkTEXT = window.frames['myEmail'].document.getElementById('p7'); 

if(checkHTML) 
{ 
var newDiv = window.frames['myEmail'].document.getElementById('iframe_p23').contentWindow.document.createElement('div'); 

newDiv.innerText = document.getElementById('contentHolder').innerText; 

window.frames['myEmail'].document.getElementById('iframe_p23').contentWindow.document.body.appendChild(newDiv); 
hasBeenUsed = true; 
} 
if(checkTEXT) 
{ 
alert("textmode"); 
window.frames['myEmail'].document.editPage.elements['p7'].innerText = document.getElementById('contentHolder').innerText; 
hasBeenUsed = true; 
} 
} 
} 


function redirect() 
{ 
//create our custom buttons in the outer shell that closes and refreshes the case window
document.getElementById('content').innerHTML +="<center><input type=\"button\" class=\"btnImportant\" id=\"btnClose\" value=\"Close \& Refresh\" onClick=\"doRefresh();\"><input type=\"button\" class=\"btnImportant\" id=\"btnDoAll\" value=\"Send \& Close \& Refresh\" onClick=\"doAll();\"></center>"; 

//get the subject of the task
var mySub = "{!Task.Subject}"; 
//filter out the # signs it causes issues
mySub = mySub.replace(/#/,""); 

//store the case id
var myCase = "{!Task.What}"; 
//store the contact id
var myContact = "{!Task.Who}"; 

//we use additional email addresses so we can send to more than 1 person at once and have it populated from the contact record
var myAddtl = ""; 
var qr = sforce.connection.query("SELECT Addt_l_Email_Addresses__c FROM Contact WHERE Id = '" + myContact + "'"); 
if(qr.size>0) 
{ 
//we found at least one, so store it
var results = qr.getArray("records"); 
myAddtl = results[0].get("Addt_l_Email_Addresses__c"); 
} 



var stringBuilder = ""; 
//start creating the iframe to hold the email page, then if content items are populated, add it to the string
stringBuilder += "<iframe id=\"myEmail\" vspace=\"0\" hspace=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"yes\" width=\"100\%\" height=\"95\%\" name=\"myEmail\" frameborder=\"0\" onLoad=\"parent.getContents()\;\" src=\"/email/author/emailauthor.jsp—retURL=\%2F{!Task.Id}"; 

if(myCase) 
{ 
stringBuilder += "\&p3_lkid=" + myCase + ""; 
} 
if(myContact) 
{ 
stringBuilder += "\&p2_lkid=" + myContact + ""; 
} 
if(myAddtl) 
{ 
stringBuilder += "\&p4=" + myAddtl + ""; 
} 
if(mySub) 
{ 
stringBuilder += "\&p6=" + mySub + ""; 
} 
//finish off the iframe tag
stringBuilder += "\"></iframe>"; 

//put the iframe into the page
document.getElementById('frameHolder').innerHTML = stringBuilder; 
} 



function pauseComp(millis) 
{ 
//it was actually too fast to refresh the case on send, so we freeze you for a sec to prevent confusion
var date = new Date(); 
var curDate = null; 

do { curDate = new Date(); } 
while(curDate-date < millis); 
} 


function doAll() 
{ 
//find out if your iframe is still on the email page, if you are, send if not, warn them and close it.
var myFrameLoc = parent.myEmail.location.href; 
var myMatch = myFrameLoc.search(/emailauthor.jsp/i); 
if(myMatch != -1) 
{ 

window.frames.myEmail.sendEmail(); 
pauseComp(1000); 
opener.location.reload(); 
window.close(); 
} 
else 
{ 
alert("You are not on the Send Email Page! This window will now close."); 
opener.location.reload(); 
window.close(); 
} 
} 


function doRefresh() 
{ 
//click the close and refresh only button does a close and refresh of the parent opening window
opener.location.reload(); 
window.close(); 
} 

</script> 
</head> 
<body onLoad = "redirect();"> 
<div id="frameHolder"></div> 
<div id="content"></div> 
<form id="myHolder" name="myHolder"> 
<textarea id="contentHolder" name="contentHolder" style="visibility:hidden;display:none;" value="">{!Task.Description}</textarea></form> 
</body> 
</html>