Import customer list from Excel file using X++ in AX 2009
To import customer list in AX 2009 from Excel sheet using X++ this code will helping you :
public void ImportCustomer()
{
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
CustTable _CustTable;
DirPartyTable dirPartyTab;
Address addTab;
//Table Declartions Ends
CustAccount AccNum= "50000";
CustAccount CustGruopId= "50000";
int iAccNum= 50000;
int row=0;
CustName vendorName;
CustName CustGroupName;
Addressing adress;
Phone phone;
TeleFax teleFax;
CustGroupId vGroupId;
smmSegmentId segmentId;
CommissSalesGroup SalesGroup;
VendCurrencyCode currency;
AddressCity city;
AddressStreet street;
CustTableAlias nameAlias;
#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();
do
{
try
{
ttsbegin;
row++;
AccNum = COMVariant2Str(cells.item(row, 3).value())+"_Ora";
CustGruopId = COMVariant2Str(cells.item(row, 1).value())+"_Ora";
vendorName = COMVariant2Str(cells.item(row, 4).value());
CustGroupName = COMVariant2Str(cells.item(row, 2).value());
adress = COMVariant2Str(cells.item(row,4).value());
// city = COMVariant2Str(cells.item(row, 7).value());
// phone = COMVariant2Str(cells.item(row, 13).value());
// teleFax = COMVariant2Str(cells.item(row, 15).value());
if(row > 1)
{
//Insert into _CustTable Table
select firstonly _CustTable where _CustTable.Name == vendorName ;
if(!_CustTable && AccNum !="1035_Ora" && AccNum!="1067_Ora" )
{
_CustTable.initValue();
_CustTable.AccountNum = AccNum;
_CustTable.Name = vendorName;
_CustTable.CustomerGroupName=CustGroupName;
_CustTable.CustomerGroupId=CustGruopId;
_CustTable.Address = adress;
//_CustTable.Phone = phone;
_CustTable.CustGroup ="MOH";
_CustTable.CustClassificationId="OTH";
//CompanyInfo::find().;
// _CustTable.VendGroup = VendGroup::find(vGroupId).VendGroup;
// _CustTable.SegmentId = smmBusRelSegmentGroup::find(segmentId).SegmentId;
_CustTable.insert();
this.UpdateCustAddressType(AccNum);
this.UpdateCustAddressName(AccNum);
// info(strfmt("Vendor uploaded successfully",_CustTable.Name));
}
else
{
_CustTable.CustomerGroupId=CustGruopId;
_CustTable.CustomerGroupName=CustGroupName;
_CustTable.update();
warning(strfmt("Customer already exists %1 : ",_CustTable.Name));
}
// Insert into InventBatch Table
// Insert into InventSerial Table
// info(strfmt("Customers(%1) uploaded successfully",_CustTable.Name));
}
ttscommit;
}
catch
{
Error(strfmt("Upload Failed in row %1", row));
}
type = cells.item(row+1, 1).value().variantType();
}while (type != COMVariantType::VT_EMPTY);
info(strfmt("Customers uploaded successfully"));
application.quit();
}
Comments