Automating email communication and email confirmations is a great way to streamline your business process.  An example of achieving this is to send out sales order confirmations when they are approved within the ERP system.

You can do this through workflows if you prefer, but in this example, we will show how you can add a small function to Dynamics AX that will automatically send the confirmations through your local SMTP server, without users having to even touch their e-mail system.

Step 1: Make Sure SMTP Parameters Are Set
The first step is to make sure the SMTP server is registered and configured correctly within Dynamics AX.  To do this, go to the System Administration – Setup – System – Email Parameters.

Step 2: Create An E-mail Template

Rather than hard code the e-mail body, we are going to take advantage of the e-mail template’s functionality that is built into Dynamics AX.  You can find the email templates in the Organization – Setup – E-Mail templates form.

After you open up the form, you will want to create a new template.

Then, create a new e-mail layout.

When you open up the layout, you can paste in the e-mail body that you want to use.

For the dynamic sections of the e-mail, place tokens surrounded by “%”.

After completing this, you can preview the e-mail.

Step 3: Find Your Form To Extend

Now we want to start creating a little bit of X++ code.  But before we do that, let’s find the form that we want to modify.  For this example, I want to override the Confirmation button on the Sales Order form.  To find the form name, I opened up the Personalization form, and it will give provide the real form name.

From there, we can open up AOT and can drill down into the ActionPane items and find the Confirm button that we want to override.

Step 4: Create A New Project

Rather than change the form directly, it’s better to wrap the code within a project.  So we will create a new project.

Now we will drag the form design from AOT over to the project.

Now we are going to add our own code to the form for the e-mail.  To do this, we will override the “clicked” method on the form.

That will open up the X++ code editor for us.  Notice that the method already has a super () function that will call down to the lower level code, so here, we just need to add our own code either before or after this function.

To begin, we will create a couple of variables to load in the Email Template, and then set a default recipient email address.

Also, after the super () function, we will perform a sendEmail.  This function will create an e-mail and send it through the default SMTP server.  This is a little different from the normal method which requires the e-mail to be sent through the user’s e-mail package.

To make this a little more dynamic, we will find the record for the customer that is referenced in the sales order, and then find their e-mail.

Finally, rather than just sending out a form letter with no personalization, we will create a mapping variable, and tell the system to replace all of the tokens that we have in the email confirmation with the values from the sales order.

Step 5: Send Your E-mail

Now we can test the function.  However, before testing, make sure the default e-mail address is assigned to the customer.

Now we will click on the email Confirmation button and see what happens.

I set a break point on the function to make sure that it’s running correctly, and it looks like it is.

Also, all of the standard logic is working as well.

If you look at the Email Sending Status in the System Administration menu, you will also be able to see the e-mail as it is queuing up to be sent.

Result: E-mail Sent

And now the customer will also receive an email confirmation without any email interaction from the customer service rep.

Very Cool!