[SOLVED] “Corrupted report PDF generated using JavaScript” in Dynamics 365 V9.0

Posted On October 9, 2018 by Clinton D'Mello Posted in Tagged in

Introduction

We had developed a functionality for Dynamics CRM v8.2 on Quote Entity by adding a custom button and on clicking a button we run a report, capture its contents using JavaScript, convert it to a PDF and attach the PDF to an Email Record.

Below shown is the code snippet

quoteInvoice = {
runReportToPrint: function () {
debugger;
var params = quoteInvoice.getReportingSession();
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";
//Calling the below function converts the report to PDF format.
quoteInvoice.convertResponseToPDF(newPth);
},
getReportingSession: function () {
var selectedIds = Xrm.Page.data.entity.getId();
selectedIds = selectedIds.replace('{', '').replace('}', '');
var strParameterXML = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'><entity name= 'quote'><all-attributes/><filter type='and'><condition attribute='quoteid' value='" + selectedIds + "' operator='eq' /></filter></entity ></fetch >";
var reportGuid = "DAF05843-CA33-E711-811E-FC15B42827EC";
var reportName = "Quote Invoice.rdl"; 
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:quoteid=" + strParameterXML);
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;
}
}

The runReportToPrint() function is called when the custom button is clicked

Issue and Error Details

Once the environment was upgraded to V9.0 we were facing issues as the PDF that was generated was corrupted as show below.

Couldnt Open PDF
Couldnt Open PDF

While debugging the code we got the following message.

Error POST Message
Error POST Message

On further research we found that the URL has been changed and  below shown is the new path that has to be used which works correctly on V9.0.

var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/ReportViewer.aspx";

Once this change was done  we did not get any error and the PDF of the report that was generated opened correctly with all of  its contents.


Share Story :

SEARCH :

FOLLOW CLOUDFRONTS BLOG :

FOLLOW CLOUDFRONTS BLOG :

Enter your email address to follow this blog and receive notifications of new posts by email.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close