How to Change Print Destination Settings Dynamically
Report print destination settings of an executing report are set using the report dialog form, e.g. by using Print Management Setup for reports such as Purchase order and Customer Invoice. However, they can also be changed dynamically, i.e. during report execution.
This can be done by injecting custom business logic at the specific point in report execution pipeline, by using a custom Docentric DSP class. This class extends the base Docentric DSP class (DocDataSourceProviderSrsReporting), which is also called Default DSP class, and overrides the overrideReportRunSettings() method.
In the overrideReportRunSettings() method you can:
- Add additional attachments to the same email that the generated report is going to be attached to.
- Change the target print destination under certain conditions, e.g. from Screen to File.
- Change the target print destination settings, e.g. change report execution context record from Purchase confirmation (VendPurchOrderJour) to Purchase orders (PurchTable). Changing context record affects saving report to Attachments and use of Standard placeholders.
- Change the selected template (aka design), e.g. if you want to use a specific template of Customer invoice under special conditions that cannot be set in Print Management Setup.
- Handle custom placeholders. Learn how >>
Let’s take a look at some examples.
Add additional email attachments and change the selected template
Sometimes we want to email report (e.g. Customer invoice) with some additional attachments (e.g. Terms and Conditions document). We can easily achieve that in the overrideReportRunSettings() method, by fetching these additional documents from Attachments, SharePoint, Azure Blob storage or our custom setup.
Also, imagine that you want to have a watermark ‘COPY’ in the Copy document of Purchase order. Let’s say that you have created a custom report design (template) with such watermark. To force the correct template selection without using Print Management Setup, you can dynamically change the selected template in the overrideReportRunSettings() method 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 |
public DocPlaceholderManager overrideReportRunSettings( DocReportRunContext _reportRunContext, boolean _replaceStandardPlaceholders = true) { DocPlaceholderManager placeholderMng; // NOTE: Call super() because of standard placeholders such as @WORKER@ and @USEREMAIL@. placeholderMng = super(_reportRunContext, _replaceStandardPlaceholders); // Add additional attachments. if (_reportRunContext.parmPrintDestination() == DocPrintDestination::Email) { // Add all files from Attachments of the underlaying purchTable as email attachments. DocuRef docuRef; while select docuRef where docuRef.RefTableId == purchTable.TableId && docuRef.RefRecId == purchTable.RecId && docuRef.TypeId == 'File' { using (System.IO.Stream docuRefStream = DocumentManagement::getAttachmentStream(docuRef)) { using (System.IO.MemoryStream emailAttachmentMs = new System.IO.MemoryStream()) { docuRefStream.Position = 0; docuRefStream.CopyTo(emailAttachmentMs); _reportRunContext.emailPrintDestSettings().addAdditionalAttachment( DocuValue::find(docuRef.ValueRecId).OriginalFileName, DocGlobalHelper::convertMemoryStreamToContainer(emailAttachmentMs)); } } } } // Dynamically change the default template: use particular template // depending on the calling report menu item. if (_reportRunContext.reportMenuItemName() == menuitemOutputStr(PurchPurchaseOrderOriginal)) // or: _reportRunContext.parmOriginalCopyType() == DocPrintMgmtOriginalCopyType::Original { _reportRunContext.parmTemplateId('PurchaseOrderOriginal'); } else if (_reportRunContext.reportMenuItemName() == menuitemOutputStr(PurchPurchaseOrderCopy)) { _reportRunContext.parmTemplateId('PurchaseOrderCopy'); } return placeholderMng; } |
Change report execution context record when saving report to Attachments
Take a look on how we can achieve, instead of attaching the Purchase order report to the VendPurchOrderJour table (displayed on the Purchase order confirmations form), to attach the report to the PurchTable table (displayed on the Purchase orders list page or the Purchase order form), or even to the VendTable table (displayed on the Vendors list page or the Vendor form):
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 |
public DocPlaceholderManager overrideReportRunSettings( DocReportRunContext _reportRunContext, boolean _replaceStandardPlaceholders = true) { DocPlaceholderManager placeholderMng; DocPrintDestSettingsFile filePrintDestSettings; placeholderMng = super(_reportRunContext, _replaceStandardPlaceholders); if (_reportRunContext.parmPrintDestination() == DocPrintDestination::File) { filePrintDestSettings = _reportRunContext.filePrintDestSettings(); if (filePrintDestSettings.parmSaveToAttachments()) { // Change the context record from VendPurchOrderJour to PurchTable. _reportRunContext.setReportExecutionContextRecord(purchTable); // Or change the context record from VendPurchOrderJour to VendTable. //_reportRunContext.setReportExecutionContextRecord(vendTable); } } return placeholderMng; } |
Read more about Saving to Attachments in the blog article >>
See also
How to Use the New Memory Print Destination >>
How to Run an SSRS Report from Code >>
How to Use Custom Placeholders in Print Destinations >>