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)

Fill in only if you are not real





The following XHTML tags are allowed: <b>, <br/>, <em>, <i>, <strong>, <u>. CSS styles and Javascript are not permitted.
Posted by Sandeep 2013-02-12 08:28

Hi,

I want to import XML by a customized button. i am using AIF Inbound port for the XML path. Kindly suggest i will click on the button XML imported in the table which is defined in the Query of the AX service.

Posted by sandeep 2013-02-12 08:21

HI

I am importing XML with same code but getting one error"Object not intialized".

Please suggest.

Posted by Ax Comeback 2011-08-11 10:46

Small correction:


the parm methods im reffering to are in Ax classes.


The fields that are missing in result xml in SalesTable, SalesLine etc. are added on the higher layers in the corresponding tables: SalesTable, SalesLine etc.



Posted by Ax Comeback 2011-08-11 10:37

Dear Erudit Team,


I have been running into some problems when running AIF Doc Serv. wizard and thought maybe you could help me.

The scenario is like this:

Example. a query (Name=QuerySalesOrder) is created based on SalesTable which is outer joined with SalesLine on SalesId, outer Joined with CustTable on CustAccount outer joined with ContactPersonsTable on ContactPersonId.The fields node in all the DataSources has a property "Dynamic" set to NO Right click on the QuerySalesOrder and start the AIF wizard.When wizard successfully completes, I am able to generate an XML schema with the fields from main and all the joined table. However some of the fields, are missing in the schema.


When I look into the AXD classes to check what parm methods get generated, I can see that exactly those fields that are missing in the xml schema, are missing there too.


So my question is, what property in AX defines which fields from the original query QuerySalesOrder will be included in the resulting schema?


I really hope you can solve this issue.



Best regards,




P.S. I am working in AX 2009

Posted by Derek 2010-12-11 05:11

Thanks a bunch for this code! This helps immensely! We don't own Aif but still need to generate XML documents for our orders as well as read them in. Save me from coding a bunch of proprietary code.

Posted by waleed 2010-06-19 13:58

I use ax2009 and can not find the form "journal types"

"10) Open Journal types form and create a new batch journal type."

thanks

mail to: ewaleed@hotmail.com

Posted by Damon 2010-01-10 15:38

hello, i have just started to play with AIF in DAX2009, but have hit a wall with the "Actions" as there does not seem to be the scan and register function on the form anymore or any buttons. do i need to run this somewhere else in this version?

Posted by morelio 2009-08-17 22:04

I would like to know how to export a project using xml

thanks

it can send the answer to moreliobb@yahoo.com.br

Posted by DaxDev 2009-03-25 05:54

Hi,

I made other operations working.

Find, FindKeys and Delete operations can be performed using the update method of Axd class.

Thanks

DaxDev

Posted by DexDev 2009-03-24 09:58

Hi,

This works well for create and read actions and I can use the same for update also. But update requires the EntityKey parameter.

Can you share some idea for other operations like Find, FindKeys and Delete.

Thanks,

DaxDev

Posted by Santosh Ramamurthy 2008-10-16 10:11

This is really a wonderful article. Well formatted and easy to understand for the guys who begin there career in AIF.

Posted by Coder 2008-09-25 21:29

Thanks a lot. Exactly the type of example I needed.

Posted by 552v 2008-06-04 23:29

Great. That's exactly what I am looking for. Thanks a lot

Posted by Studio Erudit team 2008-03-21 19:10

Thank you for your interest.

We are working on it now and are on the final phase. It should be available during next couple of days.

Posted by Anonymous 2008-03-13 07:41

Hi, I want to know when you will be putting up the 2nd article. This has nhelp me much. Thanks, but I aslo want to implement the Import and export data in XML format using AIF framework. Thanks again.