Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

(0) ShareShare
ReportReport
Posted on by 1,505

Hi All,

I am using Dynamic CRM 2016 online. For quote, On approval, I want the system to generate a PDF of quote report, attach the PDF and email it to the Customer.

Better I would like, When approver, clicks on the approve button, the system should auto generate a PDF of quote report, attach the PDF and email it to the Customer, without any further input from the user. If its not possible, I may have to put button on quote form.

Thanks. and waiting for your valuable comments.

*This post is locked for comments

  • RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hello Bharat

    encodePdf method not working

    function encodePdf(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);

    createNote(bdy);

    }

    };

    retrieveEntityReq.send();

    }

  • Anchal Kumar Profile Picture
    Anchal Kumar 85 on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hello M.Aleem Akbar  i found this error while open a fetch xml report using js.

    test.crm4.dynamics.com/.../errorhandler.aspx;ErrorCode=0x80040216&Parm0=%0d%0a%0d%0aDetalles%20del%20error%3a%20The%20report%20execution%20repl51vgqoms4g3fjynorp45%20has%20expired%20or%20cannot%20be%20found.&RequestUri=%2fReserved.ReportViewerWebControl.axd%3fReportSession%3drepl51vgqoms4g3fjynorp45%26Culture%3d1033%26CultureOverrides%3dTrue%26UICulture%3d1033%26UICultureOverrides%3dTrue%26ReportStack%3d1%26ControlID%3de440105867d249ce8a45696677d42bcb%26OpType%3dExport%26FileName%3dValua%2520Groups_Roma%25205D%2520%25204N%2520-%2520Avi%25C3%25B3%2520_Valua%2520Travel%26ContentDisposition%3dOnlyHtmlInline%26Format%3dPDF&user_lcid=3082

  • Sagar2016 Profile Picture
    Sagar2016 40 on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hi All,

    how can I run generate report for 10 records at a time to generate only one pdf for all. I m able to generate pdf for single record.

    Where should I do changes?

    Code:

    var selectedIds = "1BA95191-EDC0-E811-A971-000D3AF49211";
    var reportName = "FoodPermitAndReceiptReport.rdl";
    var reportGuid = "fced3935-f2a5-e811-8154-480fcff4f6b1"; //OR Report GUID - Replace with your report GUID
    var params = new Array();

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

    var retrieveEntityReq = new XMLHttpRequest();
    retrieveEntityReq.open("POST", pth, false);
    retrieveEntityReq.setRequestHeader("Accept", "*/*");
    retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    retrieveEntityReq.onreadystatechange = function () {
    if (this.readyState === 4) {
    retrieveEntityReq.onreadystatechange = null;
    if (this.status === 200) {
    var x = retrieveEntityReq.responseText.lastIndexOf("ReportSession=");
    var y = retrieveEntityReq.responseText.lastIndexOf("ControlID=");

    params[0] = retrieveEntityReq.responseText.substr(x + 14, 24);
    params[1] = retrieveEntityReq.responseText.substr(x + 10, 32);

    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, "_target");
    } else {
    // error
    }
    }
    };
    var preFilter = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='fs_invoice'><all-attributes/><filter type='and'><condition attribute='fs_invoiceid' operator='eq' value='" + selectedIds + "' /></filter></entity></fetch>";
    var body = "id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:CRM_Filteredfs_invoice=" + preFilter;
    retrieveEntityReq.send(body);

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hi Bharat,

    Thanks. I have seen that article - it also uses js to render the report. I haven't yet found any references that it can be done in c# for crm online, may be it cannot be done in C# and JS is the only way?

    The JS solution will be ok if running for one record, but we have requirement to run a report for selected records (could be hundreds), i.e. for each record, render report as pdf and email it out. Performance might be an issue if doing through js.

    Regards,

    Joe Lim

  • BharatPremji Profile Picture
    BharatPremji 2,485 on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hi Joe,

    I have never done it myself.

    Have a look at this article, see if it helps:

    community.dynamics.com/.../how-to-send-ssrs-report-as-a-pdf-in-email-in-dynamics-crm-online

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hi Bharat,

    Great post. Is it possible to achieve this using c# inside a plugin/custom workflow activity for Dynamics 365 online, in particular the rendering of report to pdf? We have some implementation that renders report to pdf for CRM 2016 on premise, using SSRS reporting service. However, we are migrating the code to CRM online, so seeking for solutions how we can achieve this for Dynamics 365 online?

    Thanks and Regards,

    Joe Lim

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hi Bharat,

    Thanks for reply.

    Currently I am triggering javascript function onClick of Custom Button in the ribbon. 

    Can you suggest how can I trigger javascript function onClick of Run Report button in the ribbon?

    Thank you..

    Raj Mungra

  • BharatPremji Profile Picture
    BharatPremji 2,485 on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hi Raj,

    Where are you running the report from?

    If you run it from the reports section you will see all records.  You need to run it from the record using the Run Report button in the ribbon

    Bharat

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hi Bharat,

    I followed your instructions and generated pdf of report,but report contains all records of entity.Any suggestions.

    Thanks in advance..

    Raj Mungra

  • AnkitSinghal Profile Picture
    AnkitSinghal 256 on at
    RE: Dynamics CRM 2016 Online how to execute Report, generate PDF and email?

    Hi Bharat,

    My work is done by using below code from your reference:

    var reportName = "Invo.rdl";  //Name of your invoice report

    var reportGuid = "9657861E-50B9-E711-A94E-000D3AF28673"; //GUID of your invoice report

    function runReportToPrint() {

       var invoicenumber = Xrm.Page.getAttribute("invoicenumber").getValue()

       if (invoicenumber != null) {

           invoicenumber = invoicenumber.substring(4, 9);

           var params = 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=" + invoicenumber + "&ContentDisposition=OnlyHtmlInline&Format=PDF";

           window.open(newPth, "_self");

           encodePdf(params);

       }

       else {

           alert("Invoice ID is Missing");

       }

    }

    function getReportingSession() {

       var recordId = Xrm.Page.data.entity.getId();

       recordId = recordId.replace('{', '').replace('}', '');

       var strParameterXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='invoice'><all-attributes /><filter type='and'><condition attribute='invoiceid' operator='eq' value='" + recordId + "' /> </filter></entity></fetch>";

       var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/reportviewer.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:CRM_invoice=" + 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;

    }

    function encodePdf(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);

               createNote(bdy);

           }

       };

       retrieveEntityReq.send();

    }

    function createNote(data) {

       var note = {};

       var recordId = Xrm.Page.data.entity.getId();

       recordId = recordId.replace('{', '').replace('}', '');

       var invoicenumber = Xrm.Page.getAttribute("invoicenumber").getValue()

       invoicenumber = invoicenumber.substring(4, 9);

       var refInvoice = new Object();

       refInvoice.LogicalName = "invoice";

       refInvoice.Id = recordId;

       note.ObjectId = refInvoice;

       note.ObjectTypeCode = refInvoice.LogicalName;

       note.Subject = "Invoice: " + invoicenumber;

       note.MimeType = "application/pdf";

       note.DocumentBody = data;

       note.FileName = 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

       );

    }

    Please mark my answer as verified if it is useful.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans