How to Dynamically Change Print Destination Settings
Print destination settings of an executing Docentric SSRS or Basic report are usually selected by using the report dialog form. They can also be changed dynamically, i.e. during report execution.
To dynamically change report execution settings (e.g. Report Caption, the Save to Archive flag, Selected Print Destination, Selected Template, etc.) we need to inject custom business logic at the specific point in the report execution pipeline.
The report execution pipeline consists of the following steps:
- Open the report dialog form (if not running a report from code or in batch or in case of an SSRS Print Management report).
- Validate the report parameters and other report settings likewise the selected print destination settings.
- Get the run-time report data source, i.e. needed data for printing the report.
- Override the report execution settings such as Selected Template, Selected Print Destination, Report Caption, Save to Archive Flag, etc. and replace Print Destination Placeholders, if any.
- Generate the report in the requested format and send it to the selected print destination.
To dynamically change selected print destination settings we need to inject custom business logic at the 4th step in the pipeline by extending the overrideReportRunSettings() method of the report DSP (Data Source Provider) class.
Since we get an instance of the DocReportRunContext class as an input parameter of the overrideReportRunSettings() method, we can affect all executing report settings which are exposed through this class. These are:
- Report Caption
- Template ID
- Company ID for selecting the template
- Language ID for selecting the template
- Currently selected print destination
- Email, File or Printer print destination settings
- Save to Archive flag
- Suppress Success Message flag
- Execute Only Data flag
- Report Execution Context Record
Take a look at the sample implementation of the overrideReportRunSettings() method:
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 |
public DocPlaceholderManager overrideReportRunSettings( DocReportRunContext _reportRunContext, boolean _replaceStandardPlaceholders = true) { DocPlaceholderManager placeHolderMng; DocReportTable docReport; DocReportTemplate docTemplate; Filename tempClientFilename; HcmBirthdayContract hcmBirthdayDc = this.getSrsRdpContract(); TransDate parameterFromDate = hcmBirthdayDc.parmFromDate(); placeHolderMng = super(_reportRunContext, _replaceStandardPlaceholders); // Task only for demo purposes: In case that the FromDate report parameter belong to May // and the selected print destination is Screen, we are going to change the selected print // destination to File, set the output format to Word document, open the report output file // after the report executes and suppress the message about successful report execution. // Also we are going to replace the currently selected template with the one which has // the Title set to 'May month template'. if (mthOfYr(parameterFromDate) == 5) { if (_reportRunContext.parmPrintDestination() == DocPrintDestination::Screen) { _reportRunContext.filePrintDestSettings().parmIsCalculatedFilename(false); tempClientFilename = DocFileMngHelper::getTempFileNameOnClient(); tempClientFilename = DocFileMngHelper::replaceFileExtension(tempClientFilename, 'docx'); _reportRunContext.filePrintDestSettings().parmGeneratedDocFilename(tempClientFilename); _reportRunContext.filePrintDestSettings().parmUseNextFilename(false); _reportRunContext.filePrintDestSettings().parmSaveToAttachments(false); _reportRunContext.filePrintDestSettings().parmOutputFileFormat(DocOutputFileFormat::DOCX); _reportRunContext.filePrintDestSettings().parmOpenGeneratedDoc(true); _reportRunContext.parmPrintDestination(DocPrintDestination::File); _reportRunContext.parmSuppressSuccessMsg(true); docReport = DocReportTable::findReportId(_reportRunContext.reportId()); select firstOnly docTemplate where docTemplate.Report == docReport.RecId && docTemplate.Title == 'May month template'; if (docTemplate) { _reportRunContext.parmTemplateLanguageId(docTemplate.LanguageID); _reportRunContext.parmTemplateCompanyId(docTemplate.CompanyId); _reportRunContext.parmTemplateId(docTemplate.TemplateId); } } } return placeHolderMng; } |
Another example shows how to change the name by adding VendAccount (a report parameter) as the suffix:
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; VendAccount paramOrderAccount = this.getParameter(#OrderAccount).parmValue(); // Replace standard placeholders in the current print destination. placeHolderMng = super(_reportRunContext, _replaceStandardPlaceholders); // Change the print destination settings if the File print destination is selected. if (_reportRunContext.parmPrintDestination() == DocPrintDestination::File) { // If the OrderAccount parameter has been set, change the output filename // to '[CurrentReportOutputFilename]_[OrderAccount]'. if (paramOrderAccount) { _reportRunContext.filePrintDestSettings().parmGeneratedDocFilenamePrefix( _reportRunContext.filePrintDestSettings().parmGeneratedDocFilenamePrefix() + '_' + paramOrderAccount); } } return placeHolderMng; } |
Using the same method and the DocReportRunContext class we can also change the Report Execution Context record. This record is otherwise automatically set through _args.record() of an SSRS controller instance. If we print report to the File print destination and we use the Save to attachments option, the report output file will be attached to the Report Execution Context record. For example, if we run the Purchase order report from the Purchase order confirmations form, the report will be attached to the VendPurchOrderJour table and it will accessible through Attachments on the Purchase order confirmations form or on any other form where this table acts as a master form data source.
Take a look 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
Walkthrough of this Example >>
Download Resources for the Whole Example >>
See More Examples >>