Since the release of Dynamics NAV 2009, the integration between the application and MS Outlook product became even tighter. New Role-Tailored clients gave users an ability to add Outlook panels to the custom role centers. This made sending and receiving emails easier and faster. However, creating and sending an email even from the Role-tailored client still required doing all the work manually: open the new mail message, type in the text and topic, select the recipients, format the text, etc.
Of course, it’s a great advantage to be able to access the mailbox directly from the ERP system, however, there is an arising need in automating the process of sending so-called “document emails” – standard emails for customers, vendors, or contacts which have fixed text and usually an attachment (invoice, order confirmation, reminder, etc.).
In this article, we are going to have a look at a few simple ways to create and send custom emails with the single click of a button. There is a set of custom parameters that can be pre-defined as a template and selected by the user at the time of sending the email.
We are going to create a custom template for the following email settings in this demo:
- Adding To, CC and BCC addresses
- Adding email topic
- Adding email body
- Custom email font and size
To make our email template solution applicable to any document, we are going to create a link between the template and the document. In this example, we would like to create custom emails for sales invoices.
In the Dynamics NAV Development Environment, open the Object Designer and design table 112. Add a new field Email Code:
Save and close the table. Now we need to create two tables that will hold our email template itself. The first table is going to be of the Header type which will contain the email custom settings, and the second one will be Lines, containing our actual email text.
In the Object Designer, click New and create a new table called Email Header with the following fields:
For the last two fields, which are Option types, open the Properties window and specify the following options as the option string accordingly:
Save and close the new table.
Create another table called Email Body with the following fields:
Select the Email Code field and open its properties. In the TableRelation property, type “Email Header” to link our email body lines to the email settings we will apply to them.
Save and close the table.
Now we need to create two new pages which the user will be using to create email templates. In the Object Designer, select Page objects and create a new page of ListPlus types called Email Card using the Page Wizard:
We will only have one General tab. Now add all the fields from our Email Header table to the FastTabs and click Finish.
Design the newly created page and add the following subpage after the fields list:
To ensure that the page links to a correct subpage, select this part and open its properties. Add the following properties:
Save and close the page. Create a new page of ListPart type called Email Subform and using the wizard, add all the fields from the source table and click Finish:
Design the Email Subform page and select the first empty line after the last field. Open the table properties and specify them as follows:
In the properties for the “Email Code” and “Line No.” fields, specify the Visible property to FALSE. Users do not need to see these fields. Save and close the page.
Now if you run the Email Card page and enter sample information, it may look like this:
Now we have the interface for entering the details for our emails. However, we still need to do some coding before all this can work.
So we’ll design Table 112 Sales Inv. Header. In the properties for the Email Code field, enter “Email Header” in the TableRelation field. Press F9 to access the code for the table.
Click View, C/AL Globals and add a new global variable called SMTP of type codeunit, codeunit SMTP Mail.
Select the Functions tab, add a new function called SendEmail and click Locals.
Add the Sales Invoice Header parameter on the Parameters tab. On the Variables tab, add the following local variables:
Close the C/AL Locals window and return to the code designer. Type the following code in your SendEmail function:
This function performs the following tasks:
- Gets the current customer email address from the Customer Card;
- Gets the email setup (addresses, font options, etc.) from our custom pages; and
- Creates an email with specified settings from the Email card and sends the email.
To complete the solution, design page 132 Posted Sales Invoice and add a new field Email Code:
Next, click View, Page Actions. Add a new page action:
Hit F9 to access the code for this page action. Click View, C/AL Locals and add a new local variable to the page OnAction() trigger:
Close the C/AL Locals window to return to the trigger code. Type in the following there:
Save and close the page.
Now, we are ready to test our solution. Open any posted sales invoice document and enter the Email Code value there (create an email template if this was not done during the design stage):
Check that you have SMTP setup completed for your Dynamics NAV installation:
On the posted sales invoice, click Send Email to send the custom email to the customer. Verify the result:
To ensure that the email is sent, it is also possible to add a message to the user, saying the email was sent successfully.
Thus, we now can create multiple templates per customer/customer group and specify the email parameters there. Also, it might be possible to develop a solution which may hold more complex custom parameters, such as current invoice no. or due date, company logo and company address as a hyperlink.
The idea of this demo was to show that with relatively few steps it is possible to simplify the process of sending regular emails for Dynamics NAV users, making it flexible yet fast in processing. We hope it helps!