Pages

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 (http://alexondax.blogspot.com/2013/09/how-to-properly-send-emails-with-built.html)

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="http://www.w3.org/1999/XSL/Transform"
    xmlns:Table="urn:www.microsoft.com/Formats/Table">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="//Table:Record[@name='CustTable']">
                    <p>
                        <xsl:for-each select="Table:Field">
                            <xsl:value-of select="@name"/>
                            <xsl:text> : </xsl:text>
                            <xsl:value-of select="."/>
                            <br/>
                        </xsl:for-each>
                    </p>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>



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)
            break;

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

        SysEmailTable::sendMail(sysEmailTable.EmailId,
                                'en-us',
                                'alex@fakeemail.com',
                                mappings,
                                '',
                                custTable.xml(), // XML HERE
                                false,
                                'admin',
                                false);
        i++;
    }
}

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

4 comments:

  1. All this time I haven't noticed the XSLT option for email templates... DOH! ^_~
    Great post, Alex!

    ReplyDelete
    Replies
    1. Thanks! The most useful part about it, I think is, that you can dig into the code and see easy ways to use the same XML/XSLT concepts in your own custom code to take advantage of the xRecord.xml() command on every piece of data to easily/quickly display/use data.

      Delete
  2. Hello Experts, i have to display an array element on email template, i have already retrived it and stored in a map enumerator but how can i define it on email template for example normaly we do like %variable%, but what should be the syntax for an Array element?

    ReplyDelete
  3. Do you know that Elizabeth Olsen is 5' 4" tall? Kylie Jenner is 171 cm tall? All the information about heights of celeb is available on celebrity heights now.

    ReplyDelete