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

Mark All for Open Cust Trans and Open Vend Trans

We have situations where there are lots of open transactions that need to be settled against each other. This can be the case if auto settlement is turned off. One solution is to add a "Mark All" button to the custOpenTrans or vendOpenTrans forms. This button "checks" the mark checkbox on every line. The user can then uncheck several lines if needed and Update to settle the lines. The code below is an example of what we used on the open vendor transaction screen. The code is very similar on the AR side. One note: I used vendTable.AccountNum in the code below. That should be generalized to work with any buffer that is passed into the open trans form. void customMarkAll() { VendTransOpen localVendTransOpen; VendTrans localVendTrans; container conSum; int linesProcessed; ; //show wait cursor startLengthyOperation(); element.lock(); //remove all prior markings specOffsetVoucher.deleteSpe...

Print Report in Microsoft Dynamics AX 2009 through X++

I am trying to print sales confirmation report on a button click which I have added on Sales Order Detail form in Microsoft Dynamics AX 2009. On click event of that button, I have written following code: void clicked() {     Args                args;     ReportRun           reportRun;     SalesFormLetter     salesFormLetter;     PrintJobSettings    printJobSettings;     CustConfirmJour     custConfirmJour;     RecordSortedList    list                = new RecordSortedList(55);     SalesTable          salesTableUpdate;     ;     SELEC...

Creating Free Text Invoice through X++ code

Job: Calling class from job to run the class public void freeTextInvoicePostTestJob() { Dialog dialog; DialogField dlgCustAcc; DialogGroup dialogPeriodLengthGroup, dialogPeriodLengthGroup1; DialogField dlgLedgerAcc; ; dialog = new Dialog("Free-Text Invoice"); dialogPeriodLengthGroup1 = dialog.addGroup('Cust Table'); dlgCustAcc = dialog.addField(typeid(CustAccount)); dialogPeriodLengthGroup = dialog.addGroup('Ledger Table'); dlgLedgerAcc = dialog.addField(typeid(LedgerAccount)); if(dialog.run()) { if(dlgCustAcc.value() && dlgLedgerAcc.value() != '') FreeTxtInvoiceCreatePost::main(dlgCustAcc.value(), dlgLedgerAcc.value()); else throw error(strfmt("Either CustAccount or LedgerAccount info is missing.")); } } Class: Which creates the free text invoice class FreeTxtInvoiceCreatePost { } static void main(CustAccount _custAccount, LedgerAccount _ledgerAccount) { CustInvoiceTable custInvoiceTable; ...