Customers import in AX 2012 from Excel JOB

static void CustomerMapping(Args _args)
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    int row = 1;
 
    #define.Filename('C:\\FileName.xlsx')
    CustTable custTable;
    CustInvoiceAddress  invoiceAddress;
    DirPartyTable dirPartyTable;
    LogisticsElectronicAddress logisticsElectronicAddress;
    DirPartyLocation    dirPartyLocation;
    DirParty dirParty ;
    DirPartyContactInfoView dirPartyContactInfoView;
 
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    try
    {
        workbooks.open(#Filename);
    }
    catch (Exception::Error)
    {
        throw error("File not found");
    }
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);//itemFromName(sheetName);
    cells = worksheet.cells();

    //Iterate through cells and get the values
    do
    {
    //Incrementing the row line to next Row
        custtable.clear();
        dirPartyTable.clear();
        dirPartyContactInfoView.clear();
     
        row++;
        ttsBegin;
        custTable.AccountNum        = cells.item(row,1).value().bStr();
        custTable.CustGroup         = cells.item(row,5).value().bStr();
        custTable.Currency          = cells.item(row,6).value().bStr();
        custTable.InvoiceAccount    = cells.item(row,7).value().bStr();
        custTable.SalesDistrictId   = cells.item(row,9).value().bStr();
        custTable.InvoiceAddress    = str2enum( invoiceAddress , cells.item(row,10).value().bStr());
        custTable.DlvMode           = cells.item(row,11).value().bStr();
        custTable.InventSiteId      = cells.item(row,12).value().bstr();
        custTable.InventLocation    = cells.item(row,13).value().bStr();
        custTable.insert();
        dirParty = new DirParty(custTable);

        select forUpdate dirPartyTable
            where dirPartyTable.RecId == custTable.Party;
        dirPartyTable.Name                  = cells.item(row,3).value().bStr();
        dirPartyTable.NameAlias             = cells.item(row,4).value().bStr();
        dirPartyTable.LanguageId            = cells.item(row,8).value().bStr();

    //for phone
    if (cells.item(row,14).value().int())
    {
        DirPartyContactInfoView.Locator = cells.item(row,14).value().bStr();
        DirPartyContactInfoView.Location = dirPartyLocation.Location;
        DirPartyContactInfoView.Type  = LogisticsElectronicAddressMethodType::Phone;
        DirPartyContactInfoView.PrivateForParty = dirPartyTable.RecId;

        dirParty.createOrUpdateContactInfo(DirPartyContactInfoView);
        logisticsElectronicAddress.Locator = cells.item(row,14).value().bStr();
        logisticsElectronicAddress.Location = dirPartyLocation.Location;
        logisticsElectronicAddress.Type  = LogisticsElectronicAddressMethodType::Email;
        logisticsElectronicAddress.PrivateForParty = dirPartyTable.RecId;
        logisticsElectronicAddress.insert();
        dirPartyTable.PrimaryContactPhone   = logisticsElectronicAddress.RecId;
    }

    if (cells.item(row,16).value().int())
    {
        DirPartyContactInfoView.Locator = cells.item(row,16).value().bStr();
        DirPartyContactInfoView.Location = dirPartyLocation.Location;
        DirPartyContactInfoView.Type  = LogisticsElectronicAddressMethodType::Telex;
        DirPartyContactInfoView.PrivateForParty = dirPartyTable.RecId;
     
        dirParty.createOrUpdateContactInfo(DirPartyContactInfoView);
        logisticsElectronicAddress.Locator = cells.item(row,16).value().bStr();
        logisticsElectronicAddress.Location = dirPartyLocation.Location;
        logisticsElectronicAddress.Type  = LogisticsElectronicAddressMethodType::Email;
        logisticsElectronicAddress.PrivateForParty = dirPartyTable.RecId;
        logisticsElectronicAddress.insert();
        dirPartyTable.PrimaryContactTelex   = logisticsElectronicAddress.RecId;
    }

    if (cells.item(row,18).value().int())
    {
        DirPartyContactInfoView.Locator = cells.item(row,18).value().bStr();
        DirPartyContactInfoView.Location = dirPartyLocation.Location;
        DirPartyContactInfoView.Type  = LogisticsElectronicAddressMethodType::Fax;
        DirPartyContactInfoView.PrivateForParty = dirPartyTable.RecId;
     
        dirParty.createOrUpdateContactInfo(DirPartyContactInfoView);
        logisticsElectronicAddress.Locator = cells.item(row,18).value().bStr();
        logisticsElectronicAddress.Location = dirPartyLocation.Location;
        logisticsElectronicAddress.Type  = LogisticsElectronicAddressMethodType::Email;
        logisticsElectronicAddress.PrivateForParty = dirPartyTable.RecId;
        logisticsElectronicAddress.insert();
        dirPartyTable.PrimaryContactFax   = logisticsElectronicAddress.RecId;
    }

    if (cells.item(row,19).value().int())
    {
        DirPartyContactInfoView.Locator = cells.item(row,19).value().bStr();
        DirPartyContactInfoView.Location = dirPartyLocation.Location;
        DirPartyContactInfoView.Type  = LogisticsElectronicAddressMethodType::Email;
        DirPartyContactInfoView.PrivateForParty = dirPartyTable.RecId;
     
        dirParty.createOrUpdateContactInfo(DirPartyContactInfoView);
        logisticsElectronicAddress.Locator = cells.item(row,19).value().bStr();
        logisticsElectronicAddress.Location = dirPartyLocation.Location;
        logisticsElectronicAddress.Type  = LogisticsElectronicAddressMethodType::Email;
        logisticsElectronicAddress.PrivateForParty = dirPartyTable.RecId;
        logisticsElectronicAddress.insert();
        dirPartyTable.PrimaryContactEmail   = logisticsElectronicAddress.RecId;
    }
    dirPartyTable.update();
    ttsCommit;
     // Loads the next row into the variant type and validating that its is empty or not
    type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);

    // quits the application
    application.quit();

}

Comments

Popular posts from this blog

D365: SSRS Report Development/ Customization

D365: X++ code to add custom lookup on worker to show specific workers team workers only

Error message when you log on to a Microsoft Dynamics AX 4.0 client: "You are not a recognized user of Microsoft Dynamics AX"