Microsoft Dynamics AX Developer Center Headlines

My Card

Tuesday, January 19, 2010

Send mail through X++ code by using the predefined e-mail templates in AX

Microsoft Dynamics AX provides the functionality to send the mail during various processes. Microsoft Dynamics AX has classes and tables to support these. You can send an e-mail by creating the message body at run time, basically a string object. In this case the end- user does not have the option to change the text in message body; this can be done via code only. However you can specify the e-mail template in AX and use that while sending the mail. You can define the message body (static text which is common for all the messages) in the e-mail template with the parameters (dynamics text which can be added at run time). You can then pass the values to the specified parameters in e-mail template at run time. This provides the flexibility to user to change the e-mail text if he needs to do so. However parameter can be changed by little modification in code.
The e-mail format can be specified at Basic Module -> Setup -> E-mail templates. Microsoft Dynamics AX Alert functionality uses the pre-defined e-mail template to send the alert notification mail.


Business Requirement:
I considered the requirement of sending the e-mail notification to employee when his absence request will get approved or rejected. Employee will register for his absence at absence registration journal located at Human Resources Module -> Journals -> Absence -> Registration form as shown below:


Once employee will transfer his request then the vacation approval request will appear for the absence approver at Human Resource -> Journals -> Absence -> Approval form as shown below:


Now the absence approver can approve or reject the request, in either case we will notify the employee by mail using the specific e-mail template stored at Basic -> Module -> Setup -> E-mail template form.



Define Email Template:
E-mail templates can be defined at Basic Module -> Setup -> E-mail template form. I have created an e-mail template ‘Blog’ which I will use in this exercise to send the mail.


Here I have defined the languages in which I want to send the mail & selected the template layout as HTML. You can define your template in either HTML or XSLT. I have used HTML layout for my blog. You need to define the template for each language.



To define a parameter you need to enclose it in % as shown above. In the above template I have defined 6 variables to which I will pass the values through code at run time.

Setup Email Parameters:
To send the e-mail I am using the SMTP server & respective port. You can define them at Administration -> Setup -> E-mail parameters form as shown below:


Implementation Details:
Note: Please follow the Dynamics AX business process guide for absence related setup.
In this article I will use employee Id 1000, name Charlie Carson to request for absence approval. I will use employee 3100, name: Tony Krijnen as an absence approver. E-mail id for both employees has been setup, & I will be fetching them while sending the mail.
To implement the absence notification to employee through mail, I have written a class named AbsenceNotification. This class has following methods:
1. New: I have overridden the new constructor in the class. This will be used to initialized HRMAbsenceTable and the approval status. For the same I am passing two parameters to constructor:
a. HRMAbsenceTableId – To initialize the HRMAbsenceTable
b. Status – a boolen true of false value, if called from HRMAbsenceApprove class, I am passing true and if called from HRMAbsenceReject class I am passing false.


2. getStartDate: This method is used to get the start date of absence request.


3. sendAbsenceNotification: The notification implementation code is written in this method. Below are the details:
a. Initialize the HRMAbsenceTrans table to get the last record in transaction table for absence request. This is required to get the end date of absence request.
b. Created a map object to pass the values to parameters I have defined in the e-mail templates.
c. Send mail by using the SysEmailTable::sendMail method, where we need to pass the e-mail template id to be used, default language, recipient mail id and the map which has the values for the e-mail template variables.

There are 2 AX classes which will be called by Dynamics AX as per the Approver’s action i.e. Approve or Reject. To approve absence request Dynamics AX uses the class HRMAbsenceApprove & to reject absence request it uses the class HRMAbsenceReject. Now we need to call the code in HRMAbsenceApprove & HRMAbsenceReject class’s mail method to send the mail at the time of approval or rejection.

The system will send the e-mail to notify the absence status to employee. It will use the e-mail setup for employee in employee contact information. Below is the snap shot of mail:


About X++:
X++ is the object oriented class based single dispatch programming language, in Microsoft Dynamics AX.

Disclaimer- I have not tested the code completely and it may have bugs in it. The code I have written is for demonstration purpose only therefore not following best practices in this example code.

Monday, January 11, 2010

Microsoft Dynamics AX Retail

Microsoft has released the Dynamics AX retail offering recently. Please find more information related to this at http://www.microsoft.com/dynamics/en/us/industries/retail.aspx.