Tuesday, November 16, 2010

Reading an XML file in AX 2009

These days there are lot of requirements on Integration with other systems like in import data and export data. One of the means is XML .
Here is a small job to read an XML file in AX

static void ReadingXMLFile(Args _args)

{
         XMLDocument XMLdoc
                        = XMLDocument::newFile      (@"C:\Users\nikhil.pujar\Desktop\Items.xml");
         int i,CountItemtags;
         ;
         XMLdoc.load(XMLdoc.toString());
         info(XMLdoc.getElementsByTagName("number").item(0).text());
         info(XMLdoc.getElementsByTagName("name").item(1).toString());
         countItemTags = xmldoc.getElementsByTagName('number').length();
         info (strfmt('Number of tags with name Item - %1', countItemTags));
        for (i = 0 ; i < countItemTags; i++)
       {
                info ("Item number :" + xmldoc.getElementsByTagName('number').item(i).text());
                info ("Item Name :" + xmldoc.getElementsByTagName('name').item(i).text());
        }
}

Creating a new Item through Code

For Creating a new Item in AX 2009 we have to make a entry in four imp tables
they are
1) InventTable
2) InventTableModule
3) InventItemLocation
4) InventTxt

Below is a simple job which will create a new Item in AX 2009

static void Item(Args _args)
{
     InventTable inventTable;
     InventTableModule inventTableModule;
     InventItemLocation inventItemLocation;
     InventTxt inventTxt;
     int counter;
     ;
     ttsbegin();
     inventTable.initValue();
     inventTable.ItemId = "NIK-1000";
     inventTable.ItemGroupId = "Television";
     inventTable.ItemName = "LCD Plasma";
     inventTable.ModelGroupId = "STD Cost";
     inventTable.DimGroupId = "N-W";
     inventTable.ItemType = ItemType::Item;
     if(inventTable.validatewrite())
    {
         inventTable.insert();
    }
    for(counter=0; counter<=3; counter++)
   {
       inventTableModule.ItemId = inventTable.ItemId;
       inventTableModule.ModuleType = counter;
       inventTableModule.initValue();
       if(inventTableModule.validatewrite())
      {
          inventTableModule.insert();
      }
   }
   inventItemLocation.ItemId = inventTable.ItemId;
   inventItemLocation.inventDimId = InventDim::inventDimIdBlank();
   inventItemLocation.initValue();
   if(inventItemLocation.validatewrite())
  {
       inventItemLocation.insert();
  }
  inventTxt.ItemId = inventTable.ItemId;
  inventTxt.Txt = "LCD Plasma Television";
  if(inventItemLocation.validatewrite())
 {
     inventTxt.insert();
 }
 ttscommit();
 info(" New Item created succesfully");
}