AIF Pipeline Example
In my last post (Top 5 AIF Development Tips and Tricks) I mentioned how you can use the AIF to transform external messages into the format that AX expects. This post elaborates upon that tip with a more detailed example.
Suppose you want to import currency exchange rates.
AX has an action for this out of the box – createListExchangeRates, and will happily accept a message similar to this:
<ExchangeRates xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/ExchangeRates">
<DocPurpose>Original>/DocPurpose>
<ExchRates>
<CurrencyCode>GBP</CurrencyCode>
<ExchRate>1.71</ExchRate>
<FromDate>2009-09-21<FromDate>
<ToDate>2009-09-22</ToDate>
</ExchRates>
</ExchangeRates>
Thats all well and good if you have control over the schemas of any inbound messages.
Most likely this is not the case, say for example you want to import the exchange rates for the euro from the european central banks daily published list (http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml).
If you try and receive this it will fail on two counts:
1) It is not wrapped in an envelope (which is used by the AIF to identify the message sender and destiation) and
2) the format of the exchange rate does not match AX schema.
To solve problem 1) you can make use of the ’Wrap XML in AIF envlope’ program available from axaptapedia here:www.axaptapedia.com/AIFEnvelopeTool
To solve 2) you can use the pipeline to specify an XSLT to transform the message, the XSLT to perform the transformation for this example is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ax="http://schemas.microsoft.com/dynamics/2006/02/documents/ExchangeRates"
xmlns:ext="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
exclude-result-prefixes="ext xs xsi xsl gesmes"
xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/ExchangeRates">
<xsl:namespace-alias stylesheet-prefix="ax" result-prefix="#default" />
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="gesmes:Envelope">
<ax:ExchangeRates>
<xsl:variable name="root" select="." />
<xsl:for-each select="$root/ext:Cube/ext:Cube">
<xsl:variable name="xrateDate" select="$root/ext:Cube/ext:Cube/@time" />
<xsl:for-each select="$root/ext:Cube/ext:Cube/ext:Cube">
<ax:ExchRates>
<xsl:attribute name="class">
<xsl:value-of select="'entity'" />
</xsl:attribute>
<ax:CurrencyCode>
<xsl:value-of select="@currency" />
</ax:CurrencyCode>
<ax:ExchRate>
<xsl:value-of select="@rate" />
</ax:ExchRate>
<ax:FromDate>
<xsl:value-of select="$xrateDate" />
</ax:FromDate>
<ax:ToDate>
<xsl:value-of select="$xrateDate" />
</ax:ToDate>
</ax:ExchRates>
</xsl:for-each>
</xsl:for-each>
</ax:ExchangeRates>
</xsl:template>
</xsl:stylesheet>
After running the AIF inbound service upon last weeks exchange rates it results in the following data being created (US Dollar highlighted):

Exchange rates AX form
Thanks for these tips – you helped us no end getting an AIF import working.
It might be useful to know that you can also add in the AX envelope using XSLT and an external transform tool (http://www.altova.com/altovaxml.html is free).
Here’s the XSLT that does this (for the create action)…
“MYDOMAIN\MYUSER”
“MYENDPOINT”
“http://schemas.microsoft.com/dynamics/2008/01/services/MyService/create”
We also found that using altovaxml to validate our import documents against the AX schema before wrapping them with the envelope helped us identify faults in the document format that AX simply would not tell us about (other than to say that XML didn;t comply with the schema).
Malcolm Burtt
November 4, 2009 at 5:57 pm
Thanks for the positive feedback Malcolm.
I’ll certainly have a look at the Altova tool you recommend.
gregondax
November 4, 2009 at 8:50 pm
I Just realised my XSLT code didn’t come through onto the previous replay so I’ll try again…
Malcolm Burtt
November 5, 2009 at 9:59 am
[...] Greg Philpott [...]
Art Of Creation – Dynamics AX Blog » Start learning about AIF
April 8, 2010 at 8:49 pm