Monday, July 13, 2015

Send Mail with Attachment in NAV 2013 R2

Hi guys,

This article explains a way for Sending an E-mail with PDF attachment in NAV 2013 R2.

Scenario : An E-mail must be sent to Customer when a Sales Invoice is posted in which Sales Invoice is attached.


Desired Output - An E-mail will be sent through Outlook which is configured on the workstation where NAV Client is installed.


For this activity, I will use a Codeunit object in which I will define 2 functions.

Step 1. Create the 1st function "DownloadToClientFileName". Open C/AL Locals of this function and define as shown below,



Now, C/AL Code of this function is as follows, (I have attached the object of this Codeunit in the link which can be found in the end of this article.)



Step 2. Create the 2nd Function in C/AL Globals with the name 'SendMail', Open C/AL Locals of this function and define as shown below,



I have used "Sales Invoice Header" Table as a parameter because I will use this function to send "Sales Invoice" (Report ID - 206) PDF to my Customer,

C/AL Code of this function as follows,



Save and Exit the Codeunit.

Step 3. Last and final step will be to call this function from Codeunit 80 i.e "Sales-Post". I am calling this function in the end of the OnRun Trigger, Likewise you may call it as per your requirement.



I have added the code highlighted in Blue in Codeunit 80. Now when I will post a Sales invoice, Lets see what happens.



Click OK to Ship and Invoice.



Select Allow & Click OK again.



Outlook dialog opens. We can choose to send mail from here, Also we can open the invoice to re-check it once.

Link to Download this codeunit > CU Send E-mail with attachment
Use this to send mails from anywhere in NAV. Customize it as per your requirement.


Thanks,
Ishwar Sharma

Post your comments and doubts, I will be glad to answer them.
#KnowledgeForNAVTechies