11

I'm trying to set up a JS button on a SFDC object record that sends an email to a recipient with a specific attachment from the Notes and Attachment related object. Would I theoretically be able to query an attachment from a specific file name and attach it to the email using a sforce.connection.sendEmail() method? See below for an example of the sendEmail method I got from this link.

sendEmail Example:
// single mail request

var singleRequest = new sforce.SingleEmailMessage();
singleRequest.replyTo = "[email protected]";
singleRequest.subject = "sent through ajax test driver";

singleRequest.plainTextBody = "this test went through ajax";
singleRequest.toAddresses = ["[email protected]"];

// 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 = "[email protected]";
massRequest.subject = "sent through ajax test driver";
massRequest.templateId = templateId;

var sendMailRes = sforce.connection.sendEmail([singleRequest, massRequest]);
1
  • Pretty simple with Apex if you are willing to create a Visualforce button instead. You can have it redirect right back to the detail page.
    – Adrian Larson
    Feb 19, 2016 at 23:44

1 Answer 1

1

Theoretically it might is possible. Although salesforce in its ajax toolkit file(connection.js) do not provide any reference to the fileAttachments, I still gave it a try. If you see the connection.js file you will find that the with every object that salesforce creates and sets the '_xsiType' property. Now this xsiType property is the same as the complexType in the enterprise WSDL.

So decided to create a new emailFileAttachment object and associated it with the singleEmailmessage. But unfortunately it errored out with the message:

No body specified in the file attachment

I tried with multiple body type : base64EncodedString and blob but still no luck. This may be remotely related to an issue mentioned here.

//I defined this function
sforce.EmailFileAttachment= function() {
};

//set the xsiType to emailFileAttachment
sforce.EmailFileAttachment.prototype = new sforce.Xml("EmailFileAttachment");
sforce.EmailFileAttachment.prototype._xsiType = "EmailFileAttachment";

//Created the object. 
var fileAttachment = new sforce.EmailFileAttachment();
fileAttachment.body = new Blob(['iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='], {type: 'application/octet-binary'});
fileAttachment.name = 'howdy.png';


var singleRequest = new sforce.SingleEmailMessage();
singleRequest.replyTo = "[email protected]";
singleRequest.subject = "sent through ajax test driver";

singleRequest.plainTextBody = "this test went through ajax";
singleRequest.toAddresses = ["[email protected]"];
singleRequest.fileAttachments = [fileAttachment]; //set the file in singleRequestObject.

var sendMailRes = sforce.connection.sendEmail([singleRequest]);
console.log('Single Email meesgae response ' + sendMailRes );

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .