How to Run a Docentric Basic Report from Code
Let’s say that we need to send an email with three different attachments of which two are Docentric Basic reports; the third attachment is an ordinary static document. We can automate this through the code as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
public static void runBasicReportFromCode(Args _args) { DocPrintReportBase docPrintReport; DocPrintReportSettings printReportSettings; DocPrintDestSettings printDestSettings; DocPrintDestSettingsEmail emailPrintDestSettings; PurchId selectedPurchId; PurchTable selectedPurchTable, purchTableSameVendor; str reportCaption, emailAttachmentName; container secondPurchOrderReportContent; DocFileOutputFileFormat secondPurchOrderReportOutputFormat; // Select a particular purchase order according to certain conditions... selectedPurchId = '000002'; select firstOnly selectedPurchTable where selectedPurchTable.PurchId == selectedPurchId; // Select another purchase order for the same vendor. select firstOnly purchTableSameVendor where purchTableSameVendor.OrderAccount == selectedPurchTable.OrderAccount && purchTableSameVendor.PurchId != selectedPurchId; // 1) Execute only the report data for the second purchase order. // Instance the Print Report Manager (PRM) class for the report. docPrintReport = DocPrintReportBase::construct(classStr(DocPrintPurchOrderPrintReport), ''); // Set the Report Execution Table context. docPrintReport.setReportExecutionContextRecord(purchTableSameVendor); // Suppress the report dialog form. docPrintReport.showDialog(false); // Set values of the report parameters. docPrintReport.setParameterValue('PurchId', purchTableSameVendor.PurchId); docPrintReport.setParameterValue('IncludeSalesTax', true); // Prepare the print report settings. printReportSettings = docPrintReport.printReportSettings(); printReportSettings.parmSaveToArchive(false); // NOTE: We are using the default report template. // Execute only the report data. docPrintReport.executeOnlyData(true); docPrintReport.run(); // Print the report to a container. secondPurchOrderReportOutputFormat = DocOutputFileFormat::DOCX; secondPurchOrderReportContent = DocGeneratorClient::printReportToContainer(printReportSettings, secondPurchOrderReportOutputFormat); // 2) Execute the report for the first purchase order and send it to the vendor email. // Create the report caption. reportCaption = strFmt('Purchase order %1', selectedPurchId); // Instance the Print Report Manager (PRM) class for the report. docPrintReport = DocPrintReportBase::construct(classStr(DocPrintPurchOrderPrintReport), reportCaption); // Set the Report Execution Table context. docPrintReport.setReportExecutionContextRecord(selectedPurchTable); // Suppress the report dialog form. docPrintReport.showDialog(false); // Set values of the report parameters. docPrintReport.setParameterValue('PurchId', selectedPurchId); docPrintReport.setParameterValue('IncludeSalesTax', true); // Prepare the print report settings. printReportSettings = docPrintReport.printReportSettings(); printDestSettings = docPrintReport.printDestSettings(); printReportSettings.parmSaveToArchive(false); printReportSettings.parmPrintDestination(DocPrintDestination::Email); // NOTE: We are using the default report template. // Prepare the Email print destination settings. emailPrintDestSettings = new DocPrintDestSettingsEmail(); printDestSettings.parmEmailPrintDestSettings(emailPrintDestSettings); emailPrintDestSettings.parmEmailTo(selectedPurchTable.vendorEmail()); emailPrintDestSettings.parmEmailSubject(reportCaption); emailPrintDestSettings.parmEmailAttachmentFileFormat(DocOutputFileFormat::PDF); // 3) Add the report for the second purchase order as an email additinal attachment. emailAttachmentName = strFmt('Purchase order %1.%2', purchTableSameVendor.PurchId, DocReportingHelper::convertToFileExtensionStr(secondPurchOrderReportOutputFormat)); emailPrintDestSettings.parmEmailAdditionalAttachments([[emailAttachmentName, secondPurchOrderReportContent]]); // 4) Add one more additional attachment that is not a report. emailPrintDestSettings.addAdditionalAttachment(@'C:\Temp\NDA.pdf'); // Execute the report. docPrintReport.run(); } |
See also
How to Send a Docentric Basic Report to Multiple Print Destinations >>
Docentric Basic Report Execution Hooks Overview >>
Basic Report Examples >>