Wednesday, June 5, 2013

AX 2012 Email templates

While performing several operations in Dynamics AX 2012. We often need to send emails to the users that includes some dynamic information that needs to be included in the email or we can say we need to generate email body on the fly. For example, if I need to send email to several worker, I would like to change the worker name for every email.

Dynamics AX 2012 offers email templates for that purpose, we can use variables in email templates and then swap their values at runtime while sending emails.

First of all, you will need to define an email template. To do this go to Organization Administration à Setup à Email templates.

Once the email templates form is open, create a new email template and give it a name of your choice and fill other fields like Sender email, Sender name, default language etc in both the grids. On the bottom grid, select HTML as Layout.

After the template is created, click on Email message button, this will give you an editor window where you can compose your message. See a sample message below.

Dear %WorkerName%
You have been assigned a project, please find the details below
Event Id: %ProjectId%
Event Name: %ProjectName%


Notice the variables defined with % sign, they will be replaced with appropriate values using the following code.

Go to the event from which you want to send the email, for example a button click event and then write following code.

SysEmailId sysEmailId = ProjParameters::find('EmailTemplateName');
Map mappings;
str recepient;
HcmWorker worker;
ProjTable projTable;

worker = HcmWorker::find(_worker);
projTable = projTable::find(_projId);

if (worker && projTable)
        recepient =;

        mappings = new Map(Types::String,Types::String);
        mappings.insert("WorkerName",; // this will replace variable with actual value.
        mappings.insert("ProjectId", projTable.ProjId);
        mappings.insert("ProjectName", projTable.Name);


That's it!!! your email will be sent with appropriate values using the mappings defined above.