Microsoft Dynamics AX FAQ

Microsoft Dynamics AX FAQ

How to import and export data in XML format using Axd classes?

AIFPosted by Studio Erudit team 2008-03-03 16:11

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.

Setup part:

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.

Blog Image

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.

Blog Image

Figure 2. Path to the Table browser menu item.

Blog Image

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.

Blog Image

Figure 4. A new query AxdMyTable.

4) Go to Tools - Development tools - Wizards - and select Axd Wizard.

Blog Image

Figure 5. Path to the Axd Wizard menu item.

5) Axd Wizard will start. In the Welcome page, click Next button.

Blog Image

Figure 6. The Welcome page of the Axd Wizard.

6) Select the AxdMyTable query created in the step 3 and click Next button.

Blog Image

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.

Blog Image

Figure 8. The Select a label and methods page of the Axd Wizard.

8) Click Generate button in the Generate code tab page.

Blog Image

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.

Blog Image

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.

Blog Image

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.

Blog Image

Figure 11. The AxdMyTable project generated by Axd Wizard.

Blog Image

Figure 12. Deletion of the failing methods cacheObject and cacheRecordRecord.

Blog Image

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.

Execution part:

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)
{
MyTable myTable;
AxdMyTable axdMyTable = AxdBase::newClassId(classnum(AxdMyTable));
AifEntityKeyList aifEntiryKeyList = new AifEntityKeyList();
AifDocumentXml aifDocumentXml;
XmlDocument xmlDocument;

// 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);
;
aifEntityKey.parmKeyDataMap(keyMap);
aifEntityKey.parmTableId(_record.TableId);
aifEntityKey.parmRecId(_record.RecId);

return aifEntityKey;
}
;

// add all records to be exported into the entity key list
while select myTable
{
aifEntiryKeyList.addEntityKey(getEntityKey(myTable));
}

// create an xml document containg the records
aifDocumentXml = axdMyTable.readList(aifEntiryKeyList,
null,
new AifEndpointActionPolicyInfo(),
new AifConstraintListCollection(),
connull());

xmlDocument = XMLDocument::newXML(aifDocumentXML);

// save the xml document on the disk
xmlDocument.save('C:\\Temp\\AxdMyTable_Outbound.xml');
}

The generated XML file will look like this:

Blog Image

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)
{
AxdMyTable axdMyTable;
AifDocumentXml xml;
XmlDocument xmlDocument;
;

// 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
axdMyTable.createList(xml,
new AifEndpointActionPolicyInfo(),
new AifConstraintListCollection());
}

The entire X++ project can be dowloaded from here.

Enjoy! smiley

  • Comments(15)//daxfaq.studioerudit.com/#post4