+ Start a Discussion
Yunus KalamYunus Kalam 

Custom Button to Send an generated email template

Hello,
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. and it is working fine. But I am not able to see the fields which is bind on template. How can i show or generate opportunity fields on email ?
Any suggestion would be helpful.
Thank you in Advance,
Yunus Kalam
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
(function() {
sforce.connection.sessionId = "{!$Api.Session_ID}";
var message = new sforce.SingleEmailMessage();
message.targetObjectId = "{!Opportunity.ContactId__c}";
message.templateId = "00X7E000000EF6w";
message.saveAsActivity = true;
var result = sforce.connection.sendEmail([message]);
  if(result[0].success == 'true') {
     alert("Email has sent succesfully...");
  } else {
     alert("Failed...");
  }
alert(result);
}());


 
Best Answer chosen by Yunus Kalam
VineetKumarVineetKumar
If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data.
WhatId is missing in your JS code.
message.whatId = "{!Opportunity.Id}";

All Answers

ArvindyadavArvindyadav
Hi Yunus,

If i understood right then you want to include opportunity fields on email template. then you need to edit the email template as you have template id message.templateId = "00X7E000000EF6w"; you can edit the template body and include those.

revert back if you have different problem.

Thanks!
 
VineetKumarVineetKumar
If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data.
WhatId is missing in your JS code.
message.whatId = "{!Opportunity.Id}";
This was selected as the best answer
Yunus KalamYunus Kalam
Thanks for your quick responses and suggestion. Specially VineetKumar its working for me now templates fields are getting populate Thanks once again :)

Regards,
Yunus Kalam
Yunus KalamYunus Kalam
Can i send email with any condition? I mean if opportunity stage = "Provisional" then send Email other wise not.