Activity › Forums › Salesforce® Discussions › How to send email using javascript on Salesforce Visualforce Page?
Tagged: Apex Code, Apex Page, Dynamic Visualforce, Email, Javascript, Salesforce AJAX Toolkit, Salesforce Apex, Salesforce SOQL, Salesforce Visualforce Page, SendMail Function
-
How to send email using javascript on Salesforce Visualforce Page?
Posted by Naman on April 30, 2016 at 6:14 pmHow to send email using javascript on Salesforce Visualforce Page?
Avnish Yadav replied 6 years, 1 month ago 3 Members · 2 Replies -
2 Replies
-
Yes you can do with Salesforce AJAX Toolkit. You can write something like this in your JavaScript. You can update the SendMail function with all the required parameters and call from the onclick action of the button.
<apex:page> <script type=”text/javascript”> var __sfdcSessionId = ‘{!GETSESSIONID()}’; </script> <script src=”../../soap/ajax/34.0/connection.js” type=”text/javascript”></script> <script type=”text/javascript”> function SendMail() { try { sforce.connection.sessionId = “{!$Api.Session_ID}”; var message = new sforce.SingleEmailMessage(); message.replyTo = “[email protected]”; message.targetObjectId = “xxxxxxxxxxxxxxxxx”; message.templateId = “xxxxxxxxxxxxxxxxx”; message.saveAsActivity = false; message.plainTextBody = “this test went through ajax”; var result = sforce.connection.sendEmail([message]); if(result[0].success == ‘true’) { alert(“Mail sent successfully”); } else { alert(“I had a booboo.”); } }catch(e) { alert(“Error Occurred: nn” + (e.message||e)); } } </script> </apex:page>