top of page

How to Send a NAV Report to a Customer as an Email Attachment

  • Bill Burton, Solution Systems, Inc.
  • Apr 11, 2016
  • 3 min read

Here’s a quick breakdown of the code you would need to generate a NAV Report for a Customer and send that report as a PDF Attachment.

In practice, I’d break this out into functions for re-usability, but I wanted them in one function to show that most of this can be accomplished using built in NAV functionality with small amounts of code.

The complete sample can be downloaded here: Codeunit 5009 Email Report as Attachment.txt

Codeunit 50009 Email Report as Attachment

The sample is self-contained in Codeunit 50009 Email Report as Attachment. OnRun simply calls the one function EmailNAVReport asking for a report on Cronus Customer 10000 ‘The Cannon Group PLC’.

EmailNAVReport('10000'); //Send to Cronus Customer 10000 'The Cannon Group PLC'

The function EmailNavReport first checks to see if the Customer E-mail has been set.

// Step 1 - Check for a valid email address

Customer.GET(Key);

IF Customer."E-Mail" = '' THEN

ERROR(EmailMissingError, Key);

I set the E-Mail field under Communication on the Customer Card for Customer 10000 ‘The Cannon Group PLC’ to my e-mail for this demo.

In the next step, we check the SMTP Mail Setup table to make sure NAV knows what server to use to send emails from

// Step 2 - Make sure the SMTP Server is setup within NAV

SMTPMailSetup.GET;

SMTPMailSetup.TESTFIELD("SMTP Server");

I used a disposable Gmail account for this, there are a variety of options. Ultimately, you just need a mail server and an account to get the job done. This is entered in the SMTP Mail Setup window in NAV found under Administration -> IT Administration -> General

The next bit of code runs Report 108 Customer – Order Detail. This choice was arbitrary, but serves as an example of how to run a report, get it filtered down to one record, in this case the customer number requested, and then save that report as a PDF to the system temporary folder.

// Step 3 - Run the NAV Report and Save as a PDF.

Customer.SETRANGE("No.",Key);

AttachmentFileName := STRSUBSTNO('%1Customer_Order_Detail_%2.pdf',TEMPORARYPATH,Key);

CLEAR(CustomerOrderDetail);

CustomerOrderDetail.SETTABLEVIEW(Customer);

IF NOT(CustomerOrderDetail.SAVEASPDF(AttachmentFileName)) THEN

ERROR(Text_FileError,'PDF report',AttachmentFileName);

The last bit uses Codeunit 400 SMTP Mail to send the email with a message box at the end to confirm the code has completed.

// Step 4 - Send the e-mail with the attachment

SenderName := 'Solution Systems';

SenderAddress := 'solsyst164@gmail.com';

Recipients := Customer."E-Mail";

Subject := 'Your Order Details';

Body := '<HTML><BODY>Please review the attached documents.<BR>';

HtmlFormatted := TRUE;

SMTPMail.CreateMessage(SenderName,

SenderAddress,

Recipients,

Subject,

Body,

HtmlFormatted);

SMTPMail.AppendBody('</BODY></HTML>');

SMTPMail.AddAttachment(AttachmentFileName, STRSUBSTNO('Customer_Order_Detail_%1.pdf', Key));

SMTPMail.Send;

MESSAGE('Email sent to: %1', Recipients);

The SMTP Mail has other functions worth exploring, but this simplified example shows the basics that can be used as the building blocks for your requirements.

The code above leads to this email and PDF attachment winding up in your customer’s mailbox.

Setup Notes

I used a disposable Gmail account for this proof of concept, but you’ll have to “allow less secure apps to access your account” if you want to do the same.

Sounds intimidating, but if you look into their reasoning (https://security.googleblog.com/2014/04/new-security-measures-will-affect-older.html), it essentially boils down to an effort to discourage people from sending usernames and/or passwords directly and switching over to a protocol such as OAuth.

I’m comfortable with this, but to keep the focus on the code and not the wider world of internet security, please do your own due diligence and work with your Partner or IT group on what e-mail servers and accounts are appropriate to use for this.

Comentários


RSS Feed
Recent Posts
Search By Categories
Search By Tags
bottom of page