Activity Forums Salesforce® Discussions How to send email using javascript on Salesforce Visualforce Page?

  • Naman

    April 30, 2016 at 6:14 pm

    How to send email using javascript on Salesforce Visualforce Page?

  • Abhinav

    May 26, 2016 at 2:29 pm

    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.

        <script type=”text/javascript”>
            var __sfdcSessionId = ‘{!GETSESSIONID()}’;
        <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));
  • Avnish Yadav

    September 30, 2018 at 5:30 am


    You can create simple js method

    function sendEmail() {
    // put code here

    and add to command button onclick event
    And why you want implement requirement without controller?

Viewing 1 - 3 of 3 posts

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos