Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Auto Generate PDF and Send Email In Dynamics 365 Online

Posted on by 256

Hi All,

I want to auto generate PDF with some fields value and send email that pdf.

Please suggest any good solution.

Thanks!

*This post is locked for comments

  • AnkitSinghal Profile Picture
    AnkitSinghal 256 on at
    RE: Auto Generate PDF and Send Email In Dynamics 365 Online

    Hi Ambar,

    please explain little bit about this code.

  • Suggested answer
    RE: Auto Generate PDF and Send Email In Dynamics 365 Online

    Please see code below ,implement this

    function runReportToPrint(selectedRecords) {

       try {

           debugger

           var selectedid = null;

           var reportid = null;

           var req = new XMLHttpRequest();

           var reportName = "Invoice Report Detail";

           req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/reports?$select=reportid&$filter=filename eq '" + reportName + ".rdl'", true);

           req.setRequestHeader("OData-MaxVersion", "4.0");

           req.setRequestHeader("OData-Version", "4.0");

           req.setRequestHeader("Accept", "application/json");

           req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

           req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

           req.onreadystatechange = function () {

               if (this.readyState === 4) {

                   req.onreadystatechange = null;

                   if (this.status === 200) {

                       debugger;

                       var results = JSON.parse(this.response);

                       if (results != null) {

                           reportid = results.value[0]["reportid"];

                           //if (selectedRecords != null && selectedRecords.length > 0) {

                           //    for (var i = 0; i < selectedRecords.length; i++) {

                           //        selectedid = selectedRecords[i];

                           //        var params = getReportingSessionhomegrid(reportName, reportid, selectedid);

                           //        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=" + reportName + "&ContentDisposition=OnlyHtmlInline&Format=PDF";

                           //        setTimeout(function () { window.open(newPth, "_self"); }, 1000);

                           //    }

                           if (selectedRecords != null && selectedRecords.length == 1) {

                               selectedid = selectedRecords[0];

                               var params = getReportingSessionhomegrid(reportName, reportid, selectedid);

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

                               setTimeout(function () { window.open(newPth, "_self"); }, 1000);

                           }

                           else if (selectedRecords != null && selectedRecords.length == 0) {

                               alert("Please select a record.");

                           }

                           else if (selectedRecords != null && selectedRecords.length > 1) {

                               alert("Please select only one record .");

                           }

                           else {

                               var params = getReportingSession(reportName, reportid);

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

                               window.open(newPth, "_self");

                           }

                       } else {

                           Xrm.Utility.alertDialog(this.statusText);

                       }

                   }

               }

           };

           req.send();

       } catch (ex) {

           throw ex;

       }

    }

    function getReportingSession(reportName, reportGuid) {

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

       selectedIds = selectedIds.replace('{', '');

       selectedIds = selectedIds.replace('}', '');

       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:invoiceid=" + selectedIds);

       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 getReportingSessionhomegrid(reportName, reportGuid, selectedrecordid) {

       // var selectedIds = Xrm.Page.data.entity.getId();

       selectedrecordid = selectedrecordid.replace('{', '');

       selectedrecordid = selectedrecordid.replace('}', '');

       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:invoiceid=" + selectedrecordid);

       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 runReportToPrintReceipt() {

       try {

           debugger;

           var reportName;

           var chequeamount = Xrm.Page.getAttribute("cm_chequeamount").getValue();

           if (chequeamount != null) {

               reportName = "Receipt_Cheque";

           }

           else {

               reportName = "Receipt_Cash";

           }

           var selectedid = null;

           var reportid = null;

           var req = new XMLHttpRequest();

           req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/reports()?$select=reportid&$filter=filename eq '" + reportName + ".rdl'", true);

           req.setRequestHeader("OData-MaxVersion", "4.0");

           req.setRequestHeader("OData-Version", "4.0");

           req.setRequestHeader("Accept", "application/json");

           req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

           req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

           req.onreadystatechange = function () {

               if (this.readyState === 4) {

                   req.onreadystatechange = null;

                   if (this.status === 200) {

                       debugger;

                       var results = JSON.parse(this.response);

                       if (results != null) {

                           //reportid = "EEE6E34A-5F1B-E711-8102-5065F38B06F1";

                           var reportid = results.value[0]["reportid"];

                           var params = getReportingSessionReceipt(reportName, reportid);

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

                           window.open(newPth, "_self");

                       }

                   } else {

                       Xrm.Utility.alertDialog(this.statusText);

                   }

               }

           };

           req.send();

       } catch (ex) {

           throw ex;

       }

    }

    function getReportingSessionReceipt(reportName, reportGuid) {

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

       selectedIds = selectedIds.replace('{', '');

       selectedIds = selectedIds.replace('}', '');

       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:ReceiptID=" + selectedIds);

       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;

    }

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Auto Generate PDF and Send Email In Dynamics 365 Online

    Hi,

    Did you check these threads, similar type of question is already answered here:

    community.dynamics.com/.../151236

    community.dynamics.com/.../205829

    Hope it will help you.

    Thanks

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans