Improved Email Templates – Creating Email Body with Dynamic Table

In D365FOE you can set up Email templates for sending email notifications for Retail orders, workflows, alerts, etc.
With Docentric AX Free Edition you can:

  • use an advanced HTML editor to format email bodies within D365FOE,
  • pick a placeholder from a list of available placeholders for the current email, if you need to create a dynamic email body.

Learn the basic stuff about Email Templates improved with Docentric >>

In this article we will demonstrate how to create and send a dynamic email body with a dynamic table, e.g. a Sales Order Confirmation email, which will contain some header/footer dynamic information such as Customer name and Sales ID but also a list of all ordered items and their prices.

First, we need to create a custom Docentric email handler class, which will provide the list of both header and line placeholders. This class can also contain logic for suppling the values for these placeholders. Then we need to set up an email template using Docentric and the created custom email handler class. In the end we will discuss options on how to send an email based on the created email template.

Create custom email handler class

Create a new X++ class that extends the DocEmailTemplateHandlerBase class and implement at least two methods: description() and defineCustomPlaceholders():

Set up email template with custom email handler class

Navigate to Organization administration -> Setup -> Email templates. Click the Docentric settings tab page and turn on the Use Docentric email editor checkbox. Then select previously created DocOrderConfirmWithLinesEmailHandler class (you have to build the belonging model first) as Class for placeholders.

After you click the Edit button located above the Email message content grid, the form with Docentric email body editor is open. You can see that the Field dropdown list contains placeholders defined by your DocOrderConfirmWithLinesEmailHandler class.

You will design the email template more easily if you switch to Full Screen.

You can finish designing your template using the email editor’s formatting features and both header and line placeholders regularly. Notice that in the custom email handler class we created placeholders for header with friendly names starting with CO (Confirmation Order) while placeholders for lines have friendly names starting with COL (Confirmation Order Line).

After we finish the whole template, we need to take some additional steps to enable dynamic lines, i.e. dynamic table.

Inserting hidden placeholders for the repeating table row

Scroll through the placeholder’s dropdown list and find two special placeholders:

  • Hidden table begin tag
  • Hidden table end tag

Insert them temporarily in the email body and follow the instructions on the screenshots below.

 

 

To enable rendering of the dynamic table these special placeholders wrapped in HTML comment tags, i.e. <%Hidden table begin tag%> and <!%Hidden table end tag%>, should enfold the repeating table row directly in HTML of the email body. That is why they will not be visible in Normal view of Docentric email body editor.

Sending emails with a dynamic table in the body

In D365 only Retail orders support such a scenario with a dynamic table in the body, and if you are using this built-in logic for sending email notifications for retail orders you will need to add two more placeholders to enable tagging of the repeating table row, in addition to all other custom placeholders in your email handler class:

  • tablebegin.salesline
  • tableend.salesline

You need to use these two placeholder instead of <!%Hidden table begin tag%> and <!%Hidden table end tag%> to wrap the repeating table row in an email body with a dynamic table, following the instructions from the previous section.

Check the RetailOENInfo built-in class – you will find there actually the whole list of all header and line placeholders for Retail orders.

However, if you want to send emails with a dynamic table in the body in some other scenarios, you can use Docentric APIs for sending emails, or write your own code.

Sending emails using Docentric APIs

You can use the DocEmailTemplateManager::sendMail() method to send emails with dynamic table in the body:

In order for this to work your custom email handler class should contain logic for supplying values for both header and line placeholders, i.e. it should implement the fillMappingsWithCustomPlaceholderValues() and fillLineMappingsWithCustomPlaceholderValues() methods:

If your custom email handler class doesn’t provide values for header and line placeholders, i.e. it doesn’t implement the fillMappingsWithCustomPlaceholderValues() and fillLineMappingsWithCustomPlaceholderValues() methods, then you first have to fill the mappings Placeholder name -> Placeholder value for both header and line placeholders, and afterwards to use the DocEmailTemplateManager::sendMail() method in order to send an email with a dynamic table in the body, as shown below.

Sending emails using your own X++ method

You can also use your own logic for sending emails with a dynamic table in the body but you need first to fill mappings for header and line placeholders using the DocEmailTemplateHandlerBase::fillMappings() method, and afterwards to replace all placeholders in the email body and subject using the DocEmailTemplateManager::generateEmailBodyAndSubjectWithDynamicLines() method.

Resources

Download custom email handler class >>
Learn about sending emails using Docentric APIs >>

Leave a Reply

Your email address will not be published. Required fields are marked *