[SalesForce] How to send email on custom button using Javascript

I have created custom button to send visualforce page url with opportunity id in opportunity detail page. How can i send URl like 'https://c.ap1.visual.force.com/apex/QuoteOrderForm?id=0069000000DOa0Q' with creating controller or page.

Best Answer

Take a look at the Salesforce AJAX Toolkit documented here. It is a JavaScript wrapper library around the Saleforce Partner API (a SOAP API). Which does provide an operation sendEmail. There is a JavaScript example of using the 'sendEmail' operation via the AJAX Toolkit here.

// single mail request

var singleRequest = new sforce.SingleEmailMessage();
singleRequest.replyTo = "jsmith@acme.com";
singleRequest.subject = "sent through ajax test driver";

singleRequest.plainTextBody = "this test went through ajax";
singleRequest.toAddresses = ["noone@nowhere.com"];

// mass mail request - need to get email template ID

var queryResponse = sforce.connection.query("select id from emailtemplate");
var templatedId = queryResponse.getArray("records")[0].Id;
var massRequest = new sforce.MassEmailMessage();
massRequest.targetObjectIds = [globalContact.id];
massRequest.replyTo = "jsmith@acme.com";
massRequest.subject = "sent through ajax test driver";
massRequest.templateId = templateId;

var sendMailRes = sforce.connection.sendEmail([singleRequest, massRequest]);

EMAIL TEMPLATE NOTE: The Salesforce sample above references an Email Template, you can create these just as you do Visualforce pages. Note that in your case you'll likely want to query for a specific one. Also if you wanted to share your Quote Visualforce page into such a a template, I've used Visualforce Components in the past to share my Visualforce markup between a VF page and a VF Email Template. You can of course just copy and paste into the VF Email Template, depending on how often your page changes this might be easier.

Related Topic