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

  • Abhinav

    Member
    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.

    <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>
  • Avnish Yadav

    Member
    September 30, 2018 at 5:30 am

    Hello,

    You can create simple js method
    `

    function sendEmail() {
    // put code here
    }

    `
    and add to command button onclick event
    `
    onclick="sendEmail()"
    `
    And why you want implement requirement without controller?

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos