Skip to main content

invent table Import Excel Data into Dynamics AX 2009

Here the code written in Command Button clicked event, and also i added the Excel format below of this post which i used.
void clicked()
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
System.DateTime ShlefDate;
FilenameOpen filename;
dialogField dialogFilename;
Dialog dialog;
//Table Declarations Starts
InventSize _InventSize;
InventBatch _InventBatch;
InventSerial _InventSerial;
InventTable _InventTable;
VendParameters _vendParameters;
//Table Declartions Ends
InventBatchId batchNumber;
InventBatchExpDate expdate;
itemId itemid;
TransDate poddate;
CertificatesofSterilization Certs;
CertificatesofAnalysis CertiAnalysis;
InventSizeId InventSize;
ConfigId _ConfigId;
InventColorId _InventColorId;
InventSiteId _InventSiteId;
WMSLocationId _WMSLocationId;
InventLocationId _InventLocationId;
WMSPalletId _WMSPalletId;
NoYesId ClosedTransactions;
NoYesId ClosedTransQty;
str pONo;
str srNo;
real quantity;
int row;
InventBatchExpDate ShelfLifeDate;
#Excel
// convert into str from excel cell value
str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
{
switch (_cv.variantType())
{
case (COMVariantType::VT_BSTR):
return _cv.bStr();
case (COMVariantType::VT_R4):
return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_R8):
return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_DECIMAL):
return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_DATE):
return date2str(_cv.date(),123,2,1,2,1,4);
case (COMVariantType::VT_EMPTY):
return “”;
default:
throw error(strfmt(“@SYS26908″, _cv.variantType()));
}
return “”;
}
;
dialog = new Dialog(“Excel Upoad”);
dialogFilename = dialog.addField(typeId(FilenameOpen));
dialog.filenameLookupFilter(["@SYS28576",#XLSX,"@SYS28576",#XLS]);
dialog.filenameLookupTitle(“Upload from Excel”);
dialog.caption(“Excel Upload”);
dialogFilename.value(filename);
if(!dialog.run())
return;
filename = dialogFilename.value();
application = SysExcelApplication::construct();
workbooks = application.workbooks();
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error(“File cannot be opened.”);
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
try
{
ttsbegin;
do
{
row++;
pONo = COMVariant2Str(cells.item(row, 1).value());
itemid = COMVariant2Str(cells.item(row,2).value());
InventSize = COMVariant2Str(cells.item(row, 3).value());
batchNumber = COMVariant2Str(cells.item(row, 4).value());
poddate = cells.item(row, 5).value().date();
expdate = cells.item(row, 6).value().date();
srNo = COMVariant2Str(cells.item(row, 7).value());
Certs = str2enum(Certs,cells.item(row, 8).value().bStr());
CertiAnalysis = str2enum(CertiAnalysis,cells.item(row, 9).value().bStr());
quantity = cells.item(row, 10).value().double();
_ConfigId = COMVariant2Str(cells.item(row, 12).value());
_InventColorId = COMVariant2Str(cells.item(row, 13).value());
_InventSiteId = COMVariant2Str(cells.item(row, 14).value());
_WMSLocationId = COMVariant2Str(cells.item(row, 15).value());
_InventLocationId = COMVariant2Str(cells.item(row, 16).value());
_WMSPalletId = COMVariant2Str(cells.item(row, 17).value());
ClosedTransactions = str2enum(ClosedTransactions,cells.item(row, 18).value().bStr());
ClosedTransQty = str2enum(ClosedTransQty,cells.item(row, 19).value().bStr());
if(row > 1)
{
//Insert into InventSize Table
select firstonly _InventSize where _InventSize.ItemId == itemid && _InventSize.InventSizeId == InventSize;
if(!_InventSize)
{
_InventSize.InventSizeId = InventSize;
_InventSize.ItemId = itemid;
_InventSize.insert();
}
else
{
warning(strfmt(“Item Id and InventSize (%1 - %2) already exists”,itemid,InventSize));
}
// Insert into InventBatch Table
_InventBatch.inventBatchId = batchNumber;
_InventBatch.itemId = itemid;
_InventBatch.prodDate = poddate;
_InventBatch.expDate = expdate;
_InventBatch.insert();
// Insert into InventSerial Table
_InventSerial.InventSerialId = srNo;
_InventSerial.ItemId = itemid;
_InventSerial.ProdDate = poddate;
_InventSerial.insert();
info(strfmt(“Item(%1) uploaded successfully”,itemid));
}
type = cells.item(row+1, 1).value().variantType();
}while (type != COMVariantType::VT_EMPTY);
application.quit();
ttscommit;
}
catch
{
Error(“Upload Failed”);
}
}

Popular posts from this blog

Dynamics Axapta: Sales Orders & Business Connector

Well, again folllowing my same idea of writting close to nothing and pasting code, I'll paste in some code to create a sales order from some basic data and the invoice it. I'll try to explain more in the future. AxaptaObject axSalesTable = ax.CreateAxaptaObject("AxSalesTable"); AxaptaRecord rcInventDim = ax.CreateAxaptaRecord("InventDim"); AxaptaRecord rcCustTable = ax.CreateAxaptaRecord("CustTable"); rcCustTable.ExecuteStmt("select * from %1 where %1.AccountNum == '" + MySalesOrderObject.CustAccount + "'"); if (MySalesOrderObject.CurrencyCode.Trim().Length == 0) MySalesOrderObject.CurrencyCode = rcCustTable.get_Field("Currency").ToString().Trim(); string sTaxGroup = rcCustTable.get_Field("taxgroup").ToString().Trim(); //set header level fields axSalesTable.Call("parmSalesName", MySalesOrderObject.SalesName.Trim()); axSalesTable.Call("parmCustAccount", M

Passing values between form and class

Class name is EmplDuplication and Form is EmplTable . void clicked() {    MenuFunction mf;    args args = new Args();    ;     args.record(EmplTable);     mf = new menufunction(identifierstr(EmplDuplication), MenuItemType::Action); mf.run(args); } Meanwhile, in the main() method of the EmplDuplication class, we need to put this Axapta x++ code to get the datasource: static void main(Args args) {     EmplDuplication EmplDuplication; EmplTable localEmplTable; ;     if(args.record().TableId == tablenum(EmplTable)) localEmplTable = args.record();     ... }