“How to” print reports to PDF and send email
“How to” print reports to PDF and send email
Hi guys, another post about my “How to” posts series.
In this case, I want to talk about this topic: “How to” print reports to PDF and send mail; we can have some methods to achieve this goal, with NAV 2017, NAV 2016 and with old NAV releases.
First Scenario: “How to” print reports to PDF
Schedule a report to print to PDF Format
1) “HOW TO” PRINT REPORTS TO PDF FORMAT
WITH NAV 2016 and mailing & sending documents process
“In NAV 2016 managing mailing & sending documents process has been improved.
Compared to the NAV 2015 version, you can send documents to printer, via email and via EDI in a single operation; you can set a default “document sending template” for each customer.
For this purpose new NAV objects have been introduced.”
Look at my old post here: https://robertostefanettinavblog.com/2016/05/30/nav-2016-documents-sending-objects-features/
WITH NAV 2017 and NST Reports Parameters
With NAV 2017, we can use NST Reports Parameters to activate “SAVEAS” function to produce reports in PDF format, we can schedule a report with PDF Output using SAVEAS function in report scheduling.
To achieve this gold you need only to:
-Activate SAVEAS function in NST report section
–Schedule a report in task scheduler
-Configure the Output Layout for report
Example:
Look at my old post here NAV 2017 Task Scheduler https://robertostefanettinavblog.com/2016/11/04/nav-2017-task-scheduler/
and look at section of post: “JOB QUEUE” IMPROVEMENTS
NST Reporting Parameters (nice for activate “SAVEAS” function for reporting scheduling)
Sample of Report Scheduling
… BUT FOR OLD NAV RELEASES ??
..but for older releases of NAV we need to produce a PDF Output using SAVEAS function after report printing, we haven’t report parameter optionrequest page available in older releases.
To achieve this gold you need only to:
-Activate SAVEAS function in report
–Schedule a report in job scheduler
NAV 2013 SAMPLE REPORT TO CREATE PDF IN BATCH MODE (in this case using report 206 Sales Posted Invoice)
Scenario
Generate PDF report with SAVEAS feature
Schedule this report on Job queue
SAVEASPDF Function (Report)
[Ok :=] REPORT.SAVEASPDF(Number, FileName[, Record]) OR
[Ok :=] ReportVariable.SAVEASPDF(FileName)
“When you call SAVEASPDF, the report is generated and saved to “FileName.” A Saving to PDF window shows the status of the process. Note that the request page will not be shown.
The FileName parameter specifies a location on the computer that is running Microsoft Dynamics NAV Server. “
Source MSDN: https://msdn.microsoft.com/en-us/library/dd301166(v=nav.70).aspx
Example
Filename := ‘C:MyReportsreport206Demo.pdf’;
ReturnValue := Report.SAVEASPDF(206, Filename, record);
Sample of PDF generator batch
Codeunit CreatePDFFromPostedInvoices()
CreatePDFFromPostedInvoices()
// READING ALL POSTED INVOICES TO PRINT PDF REPORTS
SalesInvoiceHeader.RESET;
SalesInvoiceHeader.SETRANGE(“PDF Generated”, FALSE);
IF SalesInvoiceHeader.FINDSET THEN BEGIN
REPEAT
Filename:= SalesInvoiceHeader.”No.” + ‘.PDF’;
Report.SAVEASPDF(206, Filename, SalesInvoiceHeader); //PDF CREATED BY SAVEAS FUNCTION
UNTIL SalesInvoiceHeader.NEXT = 0;
END;
2) “HOW TO” EMAILING PDF REPORTS
Second Scenario: “How to” emailing PDF reports
-Send generated PDF reports via email
Sendmail with NAV SMTP Sendmail Codeunit (NAV .NET integration)
To send email from NAV (OnDemand or Scheduled), you can use standard NAV SMTP Codeunit: Codeunit 400 SMTP Mail.
This codeunit use one .NET object with some functions
Mail DotNet Microsoft.Dynamics.Nav.SMTP.SmtpMessage.’Microsoft.Dynamics.Nav.SMTP’
Functions: CreateMessage, TrySend, Send
Example SMTP SENDMAIL TEST
Subject := ‘SUBJECT SAMPLE TEXT’ ;
Body := ‘BODY SAMPLETEXT’;
SMTP.CreateMessage(‘SENDER’, ‘info@Cronus.it’, ‘r.s@xxxxx.it’, Subject, Body, TRUE);
Body := Body + ‘ OTHER’;
SMTP.AppendBody(Body);
SMTP.Send;
MESSAGE(‘Mail Sended’);
Sendmail with external .NET Integration Solution
Some time ago I published a project solution on MSDN TechNet.
Solution: “Send e-mail with attachment and templates – My Script on MSDN Gallery,a short C# script to send e-mail with attachment and template, DLL .NET usable on Windows Systems.
Function: SetMail(string parfrom, string parto, string parcc, string parsubject, string partextmessage, string parpathattach, stringparpathtemplate)
https://gallery.technet.microsoft.com/scriptcenter/0c32d24a-3651-4fe1-8d10-80f9c6440a70
MY NEW SENDMAIL 2.0 PROJECT WITH WS INTEGRATION
Sendmail 2.0 C# Project
New Features:
Complete package solution (PRINT REPORTS TO PDF+SEND EMAIL INTEGRATION)
-NAV Web Services integration
-Best mailing errors check and logging.
SOON AVAILABLE: look at MSDN Scripting gallery section or on my GitHUb page soon.
I want to Email multiple vendors every morning at so what will be the CAL code for it and further steps.