Hi Experts,
I am having Transaction Form, which is having Header with respective lines. My task is to import data from excel to AX 2012(Through Code Only) by validating header with respective lines. Also I am having auto number sequence field in Header. How to do this? Please give me your valuable ideas. And I am having some simple X++ Code for import data to single table through form by clicked method. I am sharing it below.
void Clicked()
{
AsciiIO asciiIO;
Filename filename;
NoYesId skipFirstLine;
Container line;
Dialog dialog;
DialogField dialogFileName, dialogSkipFirstLine;
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
Name name;
ProductType PType;
int row;
int _PTypeId;
str _PType;
str _description;
;
dialog = new Dialog("Import file");
dialogFileName = dialog.addField(extendedTypeStr(Filenameopen), "File name");
dialog.run();
if (dialog.run())
{
filename = dialogFileName.value();
}
asciiIO = new AsciiIO(filename, 'R');
if (!asciiIO || asciiIO.status() != IO_Status::Ok )
{
throw error (strfmt("No Path Specified",filename));
}
ttsbegin;
asciiIO.inRecordDelimiter('\r\n');
asciiIO.inFieldDelimiter(',');
while (asciiIO.status() == IO_status::Ok)
{
line = asciiIO.read();
application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = dialogFileName.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); //worksheet Number
cells = worksheet.cells();
do
{
row++;
_PTypeId = any2int(cells.item(row, 1).value().toString());
_PType = cells.item(row, 2).value().bStr();
_description = cells.item(row, 3).value().bStr();
PType.productTypeId = _PTypeId;
PType.productType = _PType;
PType.description = _description;
PType.insert();
type = cells.item(row+2, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
}
ttscommit;
super();
ProductType_1_ds.reread();
ProductType_1_ds.refresh();
ProductType_1_ds.research(true);
}
Could anyone please provide code for data import((Header and Lines) from excel to ax 2012(Transaction Form).