Thursday, 7 November 2013

X++ Code for importing Movement Journal from excel in ax 2009

static void createInventoryJournal_sever(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;
date date1;
SysOperationProgress simpleProgress;
Container filterCriteria;
#avifiles
;
date1 = mkdate(30,10,2013);
sysExcelApp = SysExcelApplication::construct();///SysExcelApplication
workbooks = sysExcelApp.workbooks();///Workbooks

Dialog = new dialog();
dialogField = dialog.addField(typeId(FileNameOpen),'File Name');
filterCriteria = ['*.xls'];
//ilterCriteria = ['*.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.JournalId = "000262_070";
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(date1);//systemdateget());
axInventJournalTrans.parmLineNum(j);

//axInventJournalTrans.parmLedgerAccountIdOffset("99999");
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, 2);
axInventJournalTrans.axInventDim().parmInventSiteId(rCell.value().bStr());*/
rCell = Cells.Item(i, 5);
axInventJournalTrans.parmQty(Rcell.value().double());
/*ell = Cells.Item(i, 5);
axInventJournalTrans.parmQty(rCell.value().double());*/
rCell = Cells.Item(i, 6);
axInventJournalTrans.parmCostPrice(rCell.value().double());
axInventJournalTrans.save();
i++;
rCell = Cells.Item(i, 2);
}
ttsCommit;
}
}

No comments:

Post a Comment