In this post we check how to import data from excel in AX 2012 using X++.
we Import data from excel and check if the record exist in table then update the field otherwise print the excel row number which has failed.
static void UpdateOldProperty(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
FileName filename;
int row =1; //ignore header row
int i=0,failed=0;
Dialog dialog;
DialogField dialogField;
BEProperties properties,properties1;
BEPropertyID propertyID;
BEOldPropertyID OldPropertyID;
;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
dialog = new Dialog("FileOpen");
dialogfield = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
dialog.run();
if (dialog.run())
{
filename = (dialogfield.value());
}
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();
startLengthyOperation();
do
{
row++;
OldPropertyID = COMVariant2Str(cells.item(row,1).value());
propertyID = BEProperties::find(COMVariant2Str(cells.item(row,2).value())).PropertyID;
if(!propertyID)
{
info(strFmt("RowNo. %1, PropertyID %2, OldPropertyID %3",row,propertyID,OldPropertyID));
failed++;
}
else
{
ttsBegin;
select forUpdate properties1 where properties1.PropertyID == propertyID;
properties1.OldPropertyID = OldPropertyID;
properties1.update();
ttsCommit;
i++;
}
type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
workbook.close();
application.quit();
endLengthyOperation();
info(strFmt("%1 Records Updated and %2 records failed",i,failed));
}
No comments:
Post a Comment
Please do not add any spam link in the comment box.