Step 1: Create a record in "Email" Entity with some subject value.
Step 2: Create a record for activityParty in "ActivityParty" Entity.
Step 3: Add the attachement of pdf in base64 to ActivityMimeAttachment and create a record in "ActivityMimeAttachment" Entity.
Step 4: Open the Email form based on the newly created activityId.
- CreateEmail: function()
- {
- var email = new Object();
- email.Subject = "Testing the sending of mail through java script";
- SDK.REST.createRecord(email, "Email", EmailCallBack, function(error)
- {
- alert(error.message);
- });
- },
- When the Email record is created with sucessfull, Then we have to create activity party
- for that email.
- // Email Call Back function
- EmailCallBack: function(result)
- {
- email1 = result;
- var activityParty = new Object();
- // Set the "party" of the ActivityParty // EntityReference of an entity this activityparty relatated to.
- activityParty.PartyId = {
- Id: Xrm.Page.context.getUserId(), // id of the the current user which becomes the sender
- LogicalName: "systemuser"
- };
- // Set the "activity" of the ActivityParty
- // EntityReference.
- activityParty.ActivityId =
- {
- Id: result.ActivityId,
- LogicalName: "email"
- };
- // Set the participation type (what role the party has on the activity).
- activityParty.ParticipationTypeMask =
- {
- Value: 1
- }; // 1 mean Sender
- SDK.REST.createRecord(activityParty, "ActivityParty", ActivityPartyCallBack, function(error) {
- alert(error.message);
- });
- },
- ActivityPartyCallBack: function(result2)
- {
- // Generate the pdf file to attached to the Email.
- var responseSession = getReportingSession();
- encodePdf(responseSession);
- },
- // create a Email record with the attachement and other parameters.
- CreateEmailAttachment: function(bdy)
- {
- //Email attachment parameters
- var activitymimeattachment = Object();
- activitymimeattachment.ObjectId = Object();
- activitymimeattachment.ObjectId.LogicalName = "email";
- activitymimeattachment.ObjectId.Id = email1.ActivityId;
- activitymimeattachment.ObjectTypeCode = "email",
- activitymimeattachment.Subject = "File Attachment";
- activitymimeattachment.Body = bdy;
- activitymimeattachment.FileName = "xyz.pdf";
- //Attachment call
- activitymimeattachment.MimeType = "application/pdf";
- SDK.REST.createRecord(activitymimeattachment, "ActivityMimeAttachment", ActivityMimeAttachmentCallBack, function(error)
- {
- alert(error.message);
- });
- },
- ActivityMimeAttachmentCallBack: function(result)
- {
- var options =
- {
- openInNewWindow: true
- };
- Xrm.Utility.openEntityForm("email", email1.ActivityId, null, options);
- },
- //Encode the binary output pdf file to create an attachement
- encodePdf: function(responseSession)
- {
- var retrieveEntityReq = new XMLHttpRequest();
- var pth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + responseSession[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + responseSession[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
- retrieveEntityReq.open("GET", pth, true);
- retrieveEntityReq.setRequestHeader("Accept", "*/*");
- retrieveEntityReq.responseType = "arraybuffer";
- retrieveEntityReq.onreadystatechange = function()
- {
- if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {
- var binary = "";
- var bytes = new Uint8Array(this.response);
- for (var i = 0; i < bytes.byteLength; i++)
- {
- binary += String.fromCharCode(bytes[i]);
- }
- var bdy = btoa(binary);
- CreateEmailAttachment(bdy);
- }
- };
- retrieveEntityReq.send();
- },
- getReportingSession: function()
- {
- var selectedIds = Xrm.Page.data.entity.getId();
- var reportName = "abc.rdl";
- var reportGuid = // Report GUID - Replace with your report GUID
- var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/QuirksReportViewer.aspx";
- var retrieveEntityReq = new XMLHttpRequest();
- retrieveEntityReq.open("POST", pth, false);
- retrieveEntityReq.setRequestHeader("Accept", "*/*");
- retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:<parameters Name In ssrs>=" + selectedIds.toLowerCase().replace(/[^a-z0-9-]/g, ''));
- // p:<parameters Name In ssrs> :Is optional when you want to have parameter.
- var x = retrieveEntityReq.responseText.lastIndexOf("ReportSession=");
- var y = retrieveEntityReq.responseText.lastIndexOf("ControlID=");
- var ret = new Array();
- ret[0] = retrieveEntityReq.responseText.substr(x + 14, 24);
- ret[1] = retrieveEntityReq.responseText.substr(x + 10, 32);
- return ret;
- },
Hope this will help in your custom development. Leave your comment if you have any query.

Bhuvanesh MohankumarPosted May 15, 2016, 12:14 PM
I believe this is useful for my friend, will update his comments.
heriberto vargasPosted Mar 31, 2016, 3:13 PM
When I'm trying to implement this code on Dynamics CRM 2015 UPD1 (ONLINE) i got next error.: ErrorCode=0x8004832C&Parm0=%0d%0a%0d%0a&Parm1=%28rsItemNotFound%29&RequestUri=%2fCRMReports%2frsviewer%2freportviewer.aspx&