Skip to main content

Notifications

Dynamics 365 Community / Blogs / CleverAX / ER: How to use Organization...

ER: How to use Organization email templates when emailing configurable business documents

Nowadays a lot of businesses go paperless and email documents such as invoice or delivery note to a customer. Usually customers would like to see some order specific details in the email subject and body, it could be order reference, invoice number, totals or something else that helps to identify what this is about. In older versions of Dynamics AX, we used to do a modification to achieve that, however now it becomes standard functionality and can be achieved with Configurable business documents.

Standard “Free Text Invoice Excel” format already has this logic implemented. In this post we will create an example that shows how to use it in any other report.

Create Organization email template

Organization administration > Setup > Organization email templates

I have created new template called “Invoice” and set the subject with:

%1 – Sales invoice %2, where %1 is a Legal entity (Company) name and %2 is the invoice number.

ER_EmailTemplate_OrgEmailTemplate

To the body I have added a bit more details such as:

%1 – Legal entity name, %2 -Currency, %3 – Invoice total amount, %4 – Customer reference.

ER_EmailTemplate_OrgEmailTemplateBody

Import report format, model and model mapping

from LCS

I have loaded standard report format from LCS for the Sales invoice report (Sales invoice (Excel)). Together with the format, Invoice model and Invoice model mapping were loaded:

ER_EmailTemplate_LoadFormatFromLCS

Create derived configuration for Invoice model mapping

We must change Invoice model mapping to contain Organization email template table in a data source and bind email subject and body from the data model with it.

I have created Derived configuration and called it “Invoice model mapping Contoso”. To do it, select Invoice model mapping and Click Create Configuration:

ER_EmailTemplate_CreateInvoiceModelMappingContoso  ER_EmailTemplate_InvoiceModelMappingContoso

Click Designer, then select Sales invoice model mapping and open designer for it:ER_EmailTemplate_DesignerSalesInvoice

Add a root container “PrintMgmt” that contains “Er” and “Tables” containers under it. Of course, you can call it as you like, but better to keep meaningful naming.ER_EmailTemplate_ModelMappingDesigner

Under Tables, add table records component “SysEmailTable” (that is a reference to Organization email templates table):ER_EmailTemplate_ModelMappingDesignerTables

ER_EmailTemplate_ModelMappingDesignerTables_TableRec

On the next step we should add calculated fields to the “Er” container, see below:ER_EmailTemplate_ModelMappingDesignerEr

LanCode – that is the language from the Customer invoice journal header.ER_EmailTemplate_ModelMappingDesignerEr_LanCode

Click Edit formula and specify below formula for the field:

‘$SalesInvoiceHeaderFooterTmp’.LanguageId

ER_EmailTemplate_ModelMappingDesignerEr_LanCodeFormula

EmailTemplate – here we select record with name “Invoice” from Organization email templates (the one we created in the very beginning).ER_EmailTemplate_ModelMappingDesignerEr_EmailTemplate

Click Edit formula and set with:

FILTER(PrintMgmt.Tables.SysEmailTable, PrintMgmt.Tables.SysEmailTable.EmailId = “Invoice”)

ER_EmailTemplate_ModelMappingDesignerEr_EmailTemplateFormula

EmailMessage – this field applies filter by language in the template selected which is equal to the language from the customer invoice journal header.

ER_EmailTemplate_ModelMappingDesignerEr_EmailMessage

Click Edit formula and set with:

FIRSTORNULL(
IF(COUNT(@.EmailTemplate)=1,
WHERE(@.EmailTemplate.'<Relations'.SysEmailMessageTable,
@.EmailTemplate.'<Relations'.SysEmailMessageTable.LanguageId =
@.LanCode),
EMPTYLIST(@.EmailTemplate.'<Relations'.SysEmailMessageTable)
)
)

ER_EmailTemplate_ModelMappingDesignerEr_EmailMessageFormula

Bind Email message added in “Er” container with Email template from the print management setting in the data model:

Subject:ER_EmailTemplate_ModelMappingDesigner_BindSubject

Body:ER_EmailTemplate_ModelMappingDesigner_BindBody

We finished with Invoice model mapping changes, and can mark this version as default and complete:

ER_EmailTemplate_ModelMappingDesigner_Complete

Create derived configuration for report format

Next set of changes are done around report format. Same as above we should create derived configuration before. I have created “Sales invoice (Excel) Contoso”.

ER_EmailTemplate_CreateInvoiceModelMappingContoso   ER_EmailTemplate_SalesInvoiceFormatContoso

 

Click on the Designer button and make following changes in the mapping:

Add a root container “Emailing” that contains TxtConfigured, TxtDefault and TxtToUse containers under it. All containers should have Subject and Body calculated fields.

The logic as follows:

  • TxtConfigured contains email template and body from the organization email template.
  • TxtDefault contains predefined email subject and body with placeholders, it can be used in case if template could not be found.
  • TxtToUse contains formula that checks whether TxtConfigured returns a value, if not, then it uses TxtDefault.

If you 100% sure that you always have the email template setup, then there is no need to add anything else except TxtConfigured.

ER_EmailTemplate_SalesInvoiceFormatContoso_Emailing

TxtConfigured

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtConfigured

Subject with the formula:

FORMAT(model.PrintMgmtSetting.EmailTemplate.Subject, model.InvoiceBase.CompanyInfo.Name, model.InvoiceBase.Id)

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtConfiguredSubjectFormula

Body with the formula:

FORMAT(model.PrintMgmtSetting.EmailTemplate.Body, model.InvoiceBase.CompanyInfo.Name, model.InvoiceBase.CurrencyCode, TEXT(model.InvoiceBase.Amount), model.InvoiceBase.CustomerRef)

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtConfiguredBodyFormula

NOTE: I have listed fields in the same sequence as it shown in the “Invoice” email template.

TxtDefault

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtDefault

Subject with the formula:

FORMAT(“%1 – Sales invoice %2”, model.InvoiceBase.CompanyInfo.Name, model.InvoiceBase.Id)

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtDefaultSubjectFormula

Body with the formula:

FORMAT(CONCATENATE(“Dear Customer, <br />Thank you for your business. Please find attached sales invoice from %1 for %2 %3, your order reference: %4 <br/>Kind regards,<br />Accounts receivable team</p>”), model.InvoiceBase.CompanyInfo.Name, model.InvoiceBase.CurrencyCode, TEXT(model.InvoiceBase.Amount), model.InvoiceBase.CustomerRef)

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtDefaultBodyFormula

NOTE: I have typed the text in the formula, however you can create labels in the model and refer to it in the formula. This will allow you to maintain translations for it.

TxtToUse

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtToUse

Subject with the formula:

IF(model.PrintMgmtSetting.EmailTemplate.Subject=””, Emailing.TxtDefault.Subject, Emailing.TxtConfigured.Subject)

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtToUseSubjectFormula

Body with the formula:

IF(model.PrintMgmtSetting.EmailTemplate.Body=””, Emailing.TxtDefault.Body, Emailing.TxtConfigured.Body)

ER_EmailTemplate_SalesInvoiceFormatContoso_TxtToUseBodyFormula

After we did all changes, we can mark version as complete:

ER_EmailTemplate_SalesInvoiceFormatContoso_Complete

Setup electronic reporting destination

Organization administration > Workspaces > Electronic reporting > Electronic reporting destinations

Create ER destination for the “Sales invoice (Excel) Contoso” format:

ER_EmailTemplate_ERDestination

Click the Settings button and configure email destination, as you can see below, I have set Subject and Body with the values from TxtToUse container:

ER_EmailTemplate_ERDestinationSettings

I also use Print management configuration with the “Customer” emailing source to identify “To” email address:

ER_EmailTemplate_ERDestinationSettingsSource

Setup print management

Accounts receivable > Setup > Forms > Form setup > Print management

Select our format in print management:

ER_EmailTemplate_PrintMgmt

To test it, you can either select existing invoice or post new one:

ER_EmailTemplate_RunSalesInvoice

Note: I have not listed settings required to configure emailing, refer to Microsot documentation for additional guidance.

The email sent out by MSDyn365FO looks as below:

ER_EmailTemplate_ResultEmail

Something that was not possible before now is a part of out of the box functionality. Good to see these useful improvements that are fulfilling old system gaps!


This was originally posted here.

Comments

*This post is locked for comments