Tuesday, December 10, 2013

How to create an XSLT to transform AX XML data for emailing

I've been asked recently about how to send emails using an XSLT with AX for formatting and sending XML data.  I must admit, I had to visit StackOverflow for some XML pointers (credit Jason Aller).

This example will show you most of what you will need so that you can customize it to your own needs.  I will be dumping a few customer records to email with HTML formatting.

This post will build off of my previous post about properly sending emails from AX (

After performing all of your email setup, go to Basic>Setup>Email Templates and create a new template (bottom section) under your desired email sender/language and choose under layout, XSLT.  Click "Template" on the right and put in your XSL and save. (Note: I couldn't click save, I had to close the form and it allowed me to save):

<xsl:stylesheet version="1.0" xmlns:xsl=""
    <xsl:output method="html"/>
    <xsl:template match="/">
                <xsl:for-each select="//Table:Record[@name='CustTable']">
                        <xsl:for-each select="Table:Field">
                            <xsl:value-of select="@name"/>
                            <xsl:text> : </xsl:text>
                            <xsl:value-of select="."/>

In my subject, I put "Customer: %AcctNum% - %AcctName%" so that I could demonstrate how you can still pass mappings.

Then here is a simple job to show how to generate and pass the XML so that it is formatted and sent:

static void Job10(Args _args)
    SysEmailTable       sysEmailTable = SysEmailTable::find('XML');
    Map                 mappings;
    CustTable           custTable;
    int                 i;
    while select custTable
        if (i>=3)

        mappings = new Map(Types::String, Types::String);
        mappings.insert('AcctNum', custTable.AccountNum);
        mappings.insert('AcctName', custTable.Name);

                                custTable.xml(), // XML HERE

Hope this helps and as always, happy DAX'ing!