Thursday 23 February 2012

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\AxUser\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());
}
}

Copying a file from one location to other location using X++

Copying a file using WINAPI Classes in AX2009

static void WinApiClassestocopythefile(Args _args)
{
#avifiles
boolean                             present;
SysOperationProgress    simpleProgress;
int i;
;
// startlengthyoperation();
SimpleProgress = SysOperationProgress::newGeneral(#aviupdate, 'Simple', 100);
if(WinAPI::fileExists("C:\\Documents and Settings\\AxUser\\MyDocuments   \\Downloads\\ganatharva.pdf"))
{
for (i=1; i<=100; i++)
{
simpleProgress.incCount();
simpleprogress.setText(strfmt("%1% Of File is being Copied",i));
sleep(200);
}
WinAPI::copyFile("C:\\Documents and Settings\\AxUser\\My Documents\\Downloads\\ganatharva.pdf","C:\\Documents and Settings\\AxUser\\My Documents\\Morphx\\ganatharva.pdf",true);
Simpleprogress.kill();
}
else
{
info("This file does not exist");
}
info("The file has been Copied :)");
info(strfmt("The filesize is %1",WinApi::fileSize("C:\\Documents and Settings\\AxUser\\My Documents\\Morphx\\ganatharva.pdf")));
info(WinAPI::getComputerName());
}

Check Credit Limit for a Sales Order

static void CreditLimit(Args _args)
{
CustCreditLimit     custCreditLimit;
SalesTable             salesTable;
;
salesTable  =   SalesTable::find("SO-101297");
info(strfmt("The SalesId is %1",salesTable.SalesId));
custCreditLimit =   custCreditLimit::construct(salesTable);
if(custCreditLimit.check())
{
info("Credit limit is OKZ");
}
}

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;
;
inventTable.initValue();
inventTable.ItemId = "KKO-1000";

if(InventTable::exist(inventTable.ItemId))
{
throw error ("Item already exist,You cannot create duplicate items");
}
else
{
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;
if(inventTableModule.validatewrite())
{
inventTableModule.insert();
}
}
inventItemLocation.ItemId = inventTable.ItemId;
inventItemLocation.inventDimId = InventDim::inventDimIdBlank();

if(inventItemLocation.validatewrite())
{
inventItemLocation.insert();
}
inventTxt.ItemId = inventTable.ItemId;
inventTxt.Txt = "LCD Plasma Television";

if(inventItemLocation.validatewrite())
{
inventTxt.insert();
}
info(" New Item created succesfully");
}

how to open a report from form with without using dialog?

static void Reports_ExecuteReportSilent(Args _args)
{
       Args                           args;
       SysReportRun            reportRun;
       ;
       args = new Args();
       args.name(reportstr(MyReport));
       reportRun = classFactory.reportRunClass(args);
       reportRun.query().interactive(false);
       reportRun.report().interactive(false);
       reportRun.setTarget(PrintMedium::Printer);
       reportRun.run();
}

Document Handling in Microsoft Dynamics AX 2009

Document Handling is very useful to attach files related to AX data to forms. It’s possible to add data at any form.

To activate this feature follow the next steps:
1. Go to BasicSetupDocument management:

image
image


2. Setup Parameters:

image


Active directory is the default path where allocate the files to attach.
Check Use Active document tables has to be activated.


image


Number sequence to numerate the attachments.

3. Define Document types:

image

For each type it’s necessary define the location folder:

image


4. Select in which forms it’s possible to attach documents in Active document tables:

image


 5. Now, we can attach documents in the above forms. For example in Employees form:

image
image


For this example, we are going to attach a Photo:


image


Select the photo and click open:

image


With the check Show file activated we can see the photo/document:


image


Also, we can open the document with the right button Open.

Con2str and str2con functions in the Global Class

Many a times we use the functions which are available in the global class of AX.
2 important methods which we normally use during the import of text file or CSV file is con2str() and str2con() functions. Both these functions have limitations of use .

With respect to con2str it is that the string returned from the container cannot exceed more than 1000 char and with respect to the str2con the limitation is that chopps of the zero added before the string for example if you add str2con(“00001,0002″,”,”) then the entry in the container would be 1 and 2 respectively at position 1 and 2 of the container . This creates an issue where the Items in the system starts with the 0.

So I twisted both the functions slightly to get our desired o/p
i.e. String greater than 1000 char with con2str() method and
inclusion of 0 with str2con() method.

Here is what I changed :

static str con2strwithchange(container c, str 10 sep = ‘,’)
{
      int idx = 0;
      int len = conlen(c);
     str tmp;
     str retStr;
     ;
     while (idx < len)
    {
          idx += 1;
          if (retStr)
          retStr += sep;
          tmp = conpeek(c,idx);
          retStr += tmp;
     }
     return retStr;
}

static container str2conChanged(str _value, str 10 _sep = ‘,’)
{
     int length = strlen(_value);
     int i = 1;
     int j = strscan(_value, _sep, 1, length);
     container ret;
     void add2Ret(str _current)
     {
      // v-artemt, 26 Jul 2004, PS#: 1741
      //if (match(”, _current))
      // ret += str2int(_current);
      //else
      ret += _current;
      }
      ;
      while (j)
      {
             add2Ret(substr(_value, i, j-i));
             i = j+1;
             j = strscan(_value, _sep, i, length);
      }
      add2Ret(substr(_value, i, length-i+1));
      return ret;
}