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
SFDC_BigDogSFDC_BigDog 

Custom Button to Send an email using email template

I have created a custom button on Opportunity. The custom button has following javascript code. When the user clicks on the button the email should be sent.I find no errors in the code. But I am not able to send any emails.
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
(function() {
sforce.connection.sessionId = "{!$Api.Session_ID}";
var message = new sforce.SingleEmailMessage();
message.replyTo = "varunreddypenna@gmail.com,varunrp@okstate.edu";
message.targetObjectId = "{!Opportunity.Id}";
message.templateId = "00Xj0000000J8sI";
var result = sforce.connection.sendEmail([message]);
  if(result[0].success) {
     alert("I sent the email as you requested, master.");
  } else {
     alert("I had a booboo.");
  }
}());

 
Sumitkumar_ShingaviSumitkumar_Shingavi
Hello,

You need to handle exception also so that you can see exact error if anything going wrong! Try below:
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}

try {
	sforce.connection.sessionId = "{!$Api.Session_ID}";
	var message = new sforce.SingleEmailMessage();
	message.replyTo = "varunreddypenna@gmail.com,varunrp@okstate.edu";
	message.targetObjectId = "{!Opportunity.Id}";
	message.templateId = "00Xj0000000J8sI";
	var result = sforce.connection.sendEmail([message]);
	
	if(result[0].success == "true") {
		alert("I sent the email as you requested, master.");
	} else {
		alert(result[0].errors);
	}
}
catch(e) {	
	alert("Error Occurred: \n\n" + (e.message||e));
}

hope this helps! If yes, then mark it as solution.