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.
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.
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:
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:
Click Designer, then select Sales invoice model mapping and open designer for it:
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.
Under Tables, add table records component “SysEmailTable” (that is a reference to Organization email templates table):
On the next step we should add calculated fields to the “Er” container, see below:
LanCode – that is the language from the Customer invoice journal header.
Click Edit formula and specify below formula for the field:
‘$SalesInvoiceHeaderFooterTmp’.LanguageId
EmailTemplate – here we select record with name “Invoice” from Organization email templates (the one we created in the very beginning).
Click Edit formula and set with:
FILTER(PrintMgmt.Tables.SysEmailTable, PrintMgmt.Tables.SysEmailTable.EmailId = “Invoice”)
EmailMessage – this field applies filter by language in the template selected which is equal to the language from the customer invoice journal header.
Click Edit formula and set with:
FIRSTORNULL(
IF(COUNT(@.EmailTemplate)=1,
WHERE(@.EmailTemplate.'<Relations'.SysEmailMessageTable,
@.EmailTemplate.'<Relations'.SysEmailMessageTable.LanguageId =
@.LanCode),
EMPTYLIST(@.EmailTemplate.'<Relations'.SysEmailMessageTable)
)
)
Bind Email message added in “Er” container with Email template from the print management setting in the data model:
Subject:
Body:
We finished with Invoice model mapping changes, and can mark this version as default and 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”.
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.
TxtConfigured
Subject with the formula:
FORMAT(model.PrintMgmtSetting.EmailTemplate.Subject, model.InvoiceBase.CompanyInfo.Name, model.InvoiceBase.Id)
Body with the formula:
FORMAT(model.PrintMgmtSetting.EmailTemplate.Body, model.InvoiceBase.CompanyInfo.Name, model.InvoiceBase.CurrencyCode, TEXT(model.InvoiceBase.Amount), model.InvoiceBase.CustomerRef)
NOTE: I have listed fields in the same sequence as it shown in the “Invoice” email template.
TxtDefault
Subject with the formula:
FORMAT(“%1 – Sales invoice %2”, model.InvoiceBase.CompanyInfo.Name, model.InvoiceBase.Id)
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)
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
Subject with the formula:
IF(model.PrintMgmtSetting.EmailTemplate.Subject=””, Emailing.TxtDefault.Subject, Emailing.TxtConfigured.Subject)
Body with the formula:
IF(model.PrintMgmtSetting.EmailTemplate.Body=””, Emailing.TxtDefault.Body, Emailing.TxtConfigured.Body)
After we did all changes, we can mark version as complete:
Setup electronic reporting destination
Organization administration > Workspaces > Electronic reporting > Electronic reporting destinations
Create ER destination for the “Sales invoice (Excel) Contoso” format:
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:
I also use Print management configuration with the “Customer” emailing source to identify “To” email address:
Setup print management
Accounts receivable > Setup > Forms > Form setup > Print management
Select our format in print management:
To test it, you can either select existing invoice or post new one:
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:
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 post is locked for comments