I have a requirement to develop such that a click on a Javascript button will send an email to a particular email address. After that it will open a new window. On it "Email Sent" will show and then disappear.

I am able to put an alert from the function. When I try using sforce.connection to send email it is not working.

Please find my code as below.

  <script src="../../soap/ajax/34.0/connection.js" type="text/javascript">
    <script type="text/javascript">
    function sendmail() {
        try {
        var __sfdcSessionId = ‘{!GETSESSIONID()}’;
        sforce.connection.sessionId = “{!$Api.Session_ID}”;
        var message = new sforce.SingleEmailMessage();
        message.replyTo = “saikatkumarneogy@gmail.com”;
        //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 {
        }catch(e) {
        alert(“Error Occurred: \n\n” + (e.message||e));

This method I am calling from the function required to send the email.

Can someone please help? Any thing like achieving it through Apex method or so will do for me.

  • 1
    Is this literally the code you have in the page? I see that you have "smart quotes" in your code, which would cause the script to not work correctly. – sfdcfox Apr 18 '18 at 13:44
  • I can get alert from the block for success i.e. "Mail sent successfully". The smart quotes are not in my code. Happened here due to copying in an editor. But email I am not getting. Am I missing any step? – Testing_SFDC Apr 18 '18 at 14:18
  • Also I am getting result as "success" "true" for email being sent. – Testing_SFDC Apr 18 '18 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.