Tuesday, November 16, 2010

Creating Inventory journals through code in ax 2009 which mainly helps during the import of opening balances

Importing of inventory opening balances will be some times a difficult task because of the Inventory dimensions. This code may help you.

The below code uses AxInventJournalTrans class for creating the inventory journal transactions.
it will automatically creates the InventDimId and InventTransId.

The code contains only 5 columns which should be order. if you have more columns change the code according to it.

static void createInventoryJournal(Args _args)///and also it will import the data from the Excel
{
InventJournalNameId inventJournalNameId = "IMov";///Assign the journal Name
AxInventJournalTrans axInventJournalTrans;
InventJournalTable inventJournalTable;
Dialog dialog;
DialogField dialogField,dialogdate;
Filename filename;
COMVariant cOMVariant;
SysExcelApplication sysExcelApp;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
SysExcelCell rCell;
int i,j,k;
SysOperationProgress simpleProgress;
Container filterCriteria;
#avifiles
;
sysExcelApp = SysExcelApplication::construct();///SysExcelApplication
workbooks = sysExcelApp.workbooks();///Workbooks

Dialog = new dialog();
dialogField = dialog.addField(typeId(FileNameOpen),'File Name');
filterCriteria = ['*.xls','*. xlsx'];//// To filter only Excel files
filterCriteria = dialog.filenameLookupFilter(filterCriteria);
dialog.run();
if(dialog.run())
fileName = dialogField.value();

cOMVariant = new COMVariant();
cOMVariant.bStr(fileName);

workBook = workBooks.add(cOMVariant);///Workbook
worksheets = Workbook.worksheets();///WorkSheets
worksheet = worksheets.itemFromNum(1);///WorkSheet
Cells = workSheet.cells();///Cells

i=2;
rCell = Cells.item(i,1);///rCell

if(fileName)
{
ttsBegin;
inventJournalTable.JournalNameId = inventJournalNameId;
inventJournalTable.initFromInventJournalName(InventJournalName::find(inventJournalNameId));
inventJournalTable.insert();
simpleProgress = SysOperationProgress::newGeneral(#aviUpdate,'Importing Transactions',100);///SysProgressOperation
while(RCell.Value().bStr() != '')
{
j++;
simpleProgress.incCount();
simpleprogress.setText(strfmt("Transaction Imported: %1",i));
sleep(10);
simpleprogress.kill();
axInventJournalTrans = new AxInventJournalTrans();
axInventJournalTrans.parmJournalId(inventJournalTable.JournalId);
axInventJournalTrans.parmTransDate(systemdateget());
axInventJournalTrans.parmLineNum(j);
rCell = Cells.Item(i, 1);
axInventJournalTrans.parmItemId(RCell.value().bStr());
rCell = Cells.Item(i, 2);
axInventJournalTrans.axInventDim().parmInventSiteId(rCell.value().bStr());
rCell = Cells.Item(i, 3);
axInventJournalTrans.axInventDim().parmInventLocationId(rCell.value().bStr());
rCell = Cells.Item(i, 4);
axInventJournalTrans.parmQty(rCell.value().double());
rCell = Cells.Item(i, 5);
axInventJournalTrans.parmCostPrice(rCell.value().double());
axInventJournalTrans.save();
i++;
rCell = Cells.Item(i, 1);
}
ttsCommit;
}
}

2 comments:

  1. I have to change the wMSLocationId.
    Do I have to use axToInventDim()?

    here my for-loop:

    for (i = 2; i < 203; i++)
    {
    axInventJournalTrans = new AxInventJournalTrans();
    axInventJournalTrans.parmJournalId(inventJournalTable.JournalId);
    axInventJournalTrans.parmTransDate(systemDateGet());
    // ItemId is column 1
    axInventJournalTrans.parmItemId(excelcells.item( i, 1 ).value().bStr());

    // excelcells.item( row, column).value().Datatype();

    axInventJournalTrans.axInventDim().parmInventSiteId(bcInventSiteId);
    axInventJournalTrans.axInventDim().parmInventLocationId(bcInventLocationId);
    axInventJournalTrans.axInventDim().parmwMSLocationId(excelcells.item( i, 4 ).value().bStr());

    axInventJournalTrans.axToInventDim().parmInventSiteId(bcInventSiteId);
    axInventJournalTrans.axToInventDim().parmInventLocationId(bcInventLocationId);
    axInventJournalTrans.axToInventDim().parmwMSLocationId(excelcells.item( i, 5 ).value().bStr());

    axInventJournalTrans.parmQty(excelcells.item( i, 6 ).value().int());

    axInventJournalTrans.save();
    }

    ReplyDelete
  2. hi all,

    i have problem with importing excel to axapta through class
    iam getting all the fields exact data but problem with time fields.
    i tried to convert using sring functoin..
    still getting "0000" place of time field in table..
    so could any one help me out of this....
    i hope i have to write a method for getting time..
    its on high priority could u help me..
    my sniplet looks like

    "http://vasanthax.wordpress.com/2010/08/19/import-excel-data-into-dynamics-ax-2009/#respond"

    thanks in advance..please reply me

    thanks,
    sri aditya









    Thanks,
    SriAditya

    ReplyDelete