We often receive questions regarding import and export data in XML format in Dynamics AX 4.
We have decided to post two articles:
1) (this article) Import and export data in XML format using Axd classes (end-user and X++ developer oriented).
2) (next article) Import and export data in XML format using AIF framework (end-user oriented).
This article explains how to use Axd-classes in order to import XML files into Dynamics AX and export Dynamics AX data into XML files. The setup part could be useful for both end-users and developers, however the execution part is mostly concerning X++ developers.
In order to import or export Dynamics AX data in XML format the following objects need to exist:
a) A table - where the data will be imported to or exported from.
b) An AxBC class - which is a wrapper for the table and it is used by Axd-framework to default data into the table fields and update the related tables.
c) A query - which is used by Axd-framework to select data from the table or from the XML file.
d) An Axd-class - which represents the XML document and it is used by Axd-framework to import or export data from and to Dynamics AX.
All these objects except the table and the query can be generated automatically using the Axd Wizard.
Let's create a table MyTable, insert several records into it and generate all the objects mentioned above using Axd Wizard. Afterwards, in the execution part, we will export the records from MyTable table into XML file and import the data from the XML file into MyTable table.
1) Create a new table MyTable.
Figure 1. A new table MyTable.
2) Using Table browser, insert two records into MyTable table. Later these records will be exported into an XML file.
Figure 2. Path to the Table browser menu item.
Figure 3. Insert two new records into MyTable table.
3) Create a new query that will be used to export the data from MyTable table into XML file and import the data from the XML file into MyTable table. The query name should start with Axd in order to easily distinguish betwean standard queries and Axd queries.
Figure 4. A new query AxdMyTable.
4) Go to Tools - Development tools - Wizards - and select Axd Wizard.
Figure 5. Path to the Axd Wizard menu item.
5) Axd Wizard will start. In the Welcome page, click Next button.
Figure 6. The Welcome page of the Axd Wizard.
6) Select the AxdMyTable query created in the step 3 and click Next button.
Figure 7. The Select query page of the Axd Wizard.
7) Select all actions to be able to import and export multiple records of the document (in our case multiple records of the MyTable table). Actually for import and export using Axd classes you don't need Find actions, but they will be needed later for the AIF import article where we will use the data from this setup.
Specify a document description in the Label field and click Next button.
Figure 8. The Select a label and methods page of the Axd Wizard.
8) Click Generate button in the Generate code tab page.
Figure 9. The Generate code page of the Axd Wizard.
9) After all required objects have been generated, the final page appears. Click Finish button.
Figure 10. The final page of the Axd Wizard.
10) If you look at Compiler output window, most likely you will find several errors in the AxMyTable class.
Figure 10. The Compiler output window after the Axd Wizard has completed.
11) In order to fix the compiler errors you need to go to the newly created project AxdMyTable, delete two methods cacheObject and cacheRecordRecord from the AxMyTable class and recompile the entire project.
Figure 11. The AxdMyTable project generated by Axd Wizard.
Figure 12. Deletion of the failing methods cacheObject and cacheRecordRecord.
Figure 13. Recompilation of the entire project.
Now all required objects have been created by Axd Wizard and we are ready to export and import the data in XML format.
1) Export data from MyTable table into XML file using AxdMyTable class.
The X++ job below exports records from MyTable table into XML file C:\Temp\AxdMyTable_Outbound.xml. Please make sure the C:\Temp directorty exists before running this job.
static void exportAxdMyTable(Args _args)
AxdMyTable axdMyTable = AxdBase::newClassId(classnum(AxdMyTable));
AifEntityKeyList aifEntiryKeyList = new AifEntityKeyList();
// convert a record to the entity key
AifEntityKey getEntityKey(Common _record)
AifEntityKey aifEntityKey = AifEntityKey::construct();
List keyFields = SysDictTable::getUniqueIndexFields(_record.TableId);
Map keyMap = SysDictTable::mapFieldIds2Values(keyFields,_record);
// add all records to be exported into the entity key list
while select myTable
// create an xml document containg the records
aifDocumentXml = axdMyTable.readList(aifEntiryKeyList,
xmlDocument = XMLDocument::newXML(aifDocumentXML);
// save the xml document on the disk
The generated XML file will look like this:
Figure 14. AxdMyTable_Outbound.xml file created by exportAxdMyTable X++ job.
2) Import data from the XML file into MyTable table using AxdMyTable class.
For testing purposes you can use the XML file AxdMyTable_Outbound.xml created by the exportAxdMytable job and simply rename it to axdMyTable_Inbound.xml.
static void importAxdMyTable(Args _args)
// create an xml document from file
xmlDocument = XmlDocument::newFile("C:\\Temp\\axdMyTable_Inbound.xml");
xml = xmlDocument.xml();
axdMyTable = AxdBase::newClassId(classnum(AxdMyTable));
// insert data from the xml document into Dynamics AX
The entire X++ project can be dowloaded from here.