Pages

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

26 comments:

  1. Hi, Ishwar Sharma. First of all, thank you for posting this tutorial.

    i am still new in NAV, and i am working on how to send email with attachment out from NAV. I've tried the tutorial step by step, but the i found error which is Language, AddCode, Eval must be define first. supposedly its a build-in function that do not have to be defined right? I hope you can help me solving this. Thank you in advance.. :)

    ReplyDelete
    Replies
    1. Hi Nur,

      Could you please be more specific on what is the issue you're facing? You can contact me directly on Skype. My ID is ishwar016. I will be more than happy to help you.

      Delete
    2. Hi Ishwar.. Thank you for your respond.

      Actually I want to build a 'sending email notification' button at a page. is it possible to do that? as your post, the recipient address has been fixed right? but in my case, i want it to be dynamic and the email and details can be retrieved from table customer and table cust. ledger entry respectively.. i have designed a report to show the details of what i want to email.. how can i do that? i hope you can help me.. thank you.. :)

      Delete
    3. Hi Nur,

      You can add an action button on the page and call SendMail function on OnAction trigger of this action button.

      The E-mail address can be dynamic. Take a text variable and assign the customer's email ID to this variable. Then assign this variable to 'To address' or 'Cc address' in the SendMail function.

      And almost anything is possible NAV, you just need to take the right direction! :-)

      Delete
    4. Hi ishwar,

      Did you mean i have to create new codeunit for SendMail or just call SendMail function that was in the existing codeunit?

      Delete
    5. Hi Nur,

      I have attached the Codeunit object in the link (https://drive.google.com/file/d/0Bx-Xm9pWnmOMcTBYTGY0azRJckU/view) mentioned at the end of the of this blog.

      This codeunit has only 1 function, You can copy this function from the codeunit and add it in your page.

      Delete
  2. I have tried this codeunit. Its almost working for my purchase order. But it showning all Purchase Order and not filtering according to card.

    ReplyDelete
    Replies
    1. Hi Priyanka,

      Please check once if the coding has some issues, or , just send me the objects in which you did the coding for sending the e-mail. Mail these objects on ishwarsharma016@gmail.com. I will have to look if your code is correct or not.

      Delete
    2. Priyanka, Were you able to resolve the issue?

      Delete
    3. I am trying the same as in NAV2013...it is not working always showing the error messages for function 'DownloadToClientFileName'

      Delete
    4. for this line...

      objScript.Language :='VBScript';
      'function RenameTempFile(fromFile, toFile)'+Cr+
      'set fso = createobject("Scripting.FileSystemObject")'+CR+
      'set x = createobject("Scripting.TypeLib")'+CR+
      'path = fso.getparentfoldername(fromfile)'+CR+
      'toPath = path+"\"+left(x.GUID,5405)'+CR+
      'fso.Createfolder toPath'+CR+
      'fso.MoveFile fromFile, toPath+"\"+tofile'+CR+
      'RenameTempFile = toPath'+CR+
      'end function');
      ClientFileNmae :=objScript.Eval('RenameTempFile("'+ClientFileNmae+'","'ToFile'")');
      ClientFileNmae :=ClientFileNmae+'\'+ToFile;

      Delete
    5. always getting a messages "U need to specified the unknown variables 'Language'"

      Delete
  3. It will work for nav 2013 or not.

    ReplyDelete
    Replies
    1. Hi Manish, I developed this for NAV 2013 R2. I am not sure if the same will help in NAV 2013. This might be helpful in the higher versions but I am not sure about the lower ones.

      Delete
    2. Thank you for your reply...
      manish

      Delete
  4. In my case, I took the objects ASIS from your blog. First time when I posted the sales invoice the two dialog boxes for starting the Outlook session came but Outlook did not start. However, when I tried to post other invoice, the dialog boxes for opening outlook session did not start.

    Is there something which I am missing.

    With Best Wishes,

    Krupesh Daiya,

    ReplyDelete
    Replies
    1. You need to check outlook settings. There is a setting in outlook which allows other apps to use outlook in the background. Please check and let me know if that helps.

      Delete
  5. Hi Ishwar thanks for your post. To send mail from posted sales invoice. I have called the code unit from action button in page action.Outlook is triggering exactly but the attachment is not there in mail. can you please help me to add attachment.

    ReplyDelete
    Replies
    1. Hi Vignesh, Thanks for your comment :)

      I think there might be a mistake in your code somewhere which is attaching the file in the mail. Can you paste the code here OR better, can you send your objects in text format to me via e-mail? Sorry but I can not help you without looking at the code.

      Delete
    2. Hi Ishwar,
      Thanks for your reply. I have sent you a mail with attached txt object. kindly check and help me.Thanks in advance.

      Delete
    3. Hi Vignesh, I received you mail. Please give me sometime. I will take a look at the objects and reply you during the weekend.

      Delete
  6. Hi Ishwar,
    While running this codeunit error comes failed due to
    80040154 class not registered
    plz helpme

    ReplyDelete