Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How I can generate a CRM report and set as an attachment notes by JavaScript?

(0) ShareShare
ReportReport
Posted on by

Hi, the following script is not working, 
but the function OpenOrderReport alone is working and the report is opening as a pop-up but when I add it to the rest of the script nothing happens; I'm calling AttachReport function on load.
Any help please ?
Thank you 

function AttachReport() 
{
  debugger;
  var params = OpenOrderReport();
  var newPth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession="+ params[0]+"&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID="+ params[1] +"&OpType=Export&FileName=public&ContentDisposition=OnlyHtmlInline&Format=PDF";  
  window.open(newPth, "_self"); 
  encodePdf(params);
}

function OpenOrderReport() 
{
debugger;
    var rdlName    = "invoice.rdl"; 
    var reportGuid = "8e547587-59e8-e611-80d7-00155d004f8b";                    
    var entityType = "1090"; //Replace
    var entityGuid = Xrm.Page.data.entity.getId();  
    var url = Xrm.Page.context.getClientUrl() + "/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=" +rdlName + "&id=%7b" + reportGuid + "%7d&records=%7b" + entityGuid.replace('{', '').replace('}', '') + "%7d&recordstype=" + entityType;
    
    //added
    var retrieveEntityReq = new XMLHttpRequest();
    retrieveEntityReq.open("POST", url, false);
    retrieveEntityReq.setRequestHeader("Accept", "*/*");
    retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    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;
    //window.open(url, null, 800, 600, true, false, null); <--- alrdy exists 
}

function encodePdf(responseSession) 
{
debugger;
alert("encode pdf");
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);
createNote(bdy);
}
};
retrieveEntityReq.send();
}


function createNote(data)
{
debugger;
alert("create new note");
var note = {};
var refInvoice = new Object();
refInvoice.LogicalName = "invoice";
refInvoice.Id = recordId;
note.ObjectId = refInvoice;
note.ObjectTypeCode = refInvoice.LogicalName;
note.Subject = "Invoice: " + new_invoicenumber;
note.MimeType = "application/pdf";
note.DocumentBody = data;
note.FileName = new_invoicenumber+ ".pdf";
XrmServiceToolkit.Rest.Create(
note,
"AnnotationSet",

function (result)
{
//Alert user
alert("Note Created");
//Refresh data so user sees newly created note
Xrm.Page.data.refresh(false);
},

function (error)
{
alert(error.message);
},
true
);
}

//==========================================End==============================//

*This post is locked for comments

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans