Friday 11 September 2015

Address Import in AX 2012

Here, I got the job for importing address masters like CountryId, State, ZipCode, City... I am sharing with DAX techies hope it will help you.

static void AddressImport(Args _args)
{
    SysExcelApplication               application;
    SysExcelWorkbooks               workbooks;
    SysExcelWorkbook                workbook;
    SysExcelWorksheets              worksheets;
    SysExcelWorksheet               worksheet;
    SysExcelCells                        cells;
    COMVariantType                  type;
    Name                                     name;
    FileName                               filename;
    InventPosting                         InventPosting;

    DimensionAttributeValueCombination DimensionAttributeValueCombination;

    int row =1;
    LogisticsAddressZipCode          zipCode;
    LogisticsAddressZipCodeId        zipCodeId;
    LogisticsAddressCity             city;
    LogisticsAddressStateId          stateId;
    LogisticsAddressState            state;
    LogisticsAddresssCity            cityRecord;
    LogisticsAddressCountyName       countyName;
    LogisticsAddressCountyId         countyId;
    LogisticsAddressCounty           county;

    boolean                          badRecord;
    int                              i=1;
    int                              numProcessedRecords=0;
    LogisticsAddressCountryRegionId  _countryRegionId;

    //LogisticsAddressStateId          stateId;
    //LogisticsAddressCountyId         countyId;
    //LogisticsAddressCountyName       countyName;
   
    application = SysExcelApplication::construct();
    workbooks   = application.workbooks();
   
    //specify the file path that you want to read
    filename    = "C:\\Users\\v-vimsin\\Documents\\Vimal\\DIXF\\LogisticsPostalAddress - Copy.xlsx";
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }
   
    ttsbegin;
    workbook    = workbooks.item(1);
    worksheets  = workbook.worksheets();
    worksheet   = worksheets.itemFromNum(1); //Here 3 is the worksheet Number
    cells       = worksheet.cells();
   
    do
    {
        row++;
        zipCodeId        = cells.item(row, 4).value().bStr();
        if (zipCodeId == "")
            zipCodeId = int2str(cells.item(row, 4).value().double());
       
        stateId          = cells.item(row, 3).value().bStr();
        city             = cells.item(row, 2).value().bStr();
        _countryRegionId = cells.item(row, 1).value().bStr();
        countyId         = cells.item(row, 5).value().bStr();
       
        if (stateId != '')
            state= LogisticsAddressState::find(_countryRegionId, stateId);  
            if (!state.RecId)
            {
                state.CountryRegionId   = _countryRegionId;
                state.StateId           = stateId;
                state.insert();
            }
            // Check if county exists
            //if(countyId != '')
            county = LogisticsAddressCounty::find(_countryRegionId, stateId, countyId);
   
            if (city != '')
            {
                select firstonly cityRecord where
                    cityRecord.Name             == city &&
                    cityRecord.StateId          == stateId &&
                    cityRecord.CountryRegionId  == _countryRegionId &&
                    cityRecord.CountyId         == countyId;
            }

            if (!cityRecord.RecId)
            {
                cityRecord.CountryRegionId  = _countryRegionId;
                cityRecord.Name             = city;
                cityRecord.StateId          = stateId;
                cityRecord.CountyId         = countyId;
                cityRecord.insert();
            }

            select firstonly zipCode where
                zipCode.ZipCode         == zipCodeId &&
                zipCode.State           == stateId &&
                zipCode.County          == countyId &&
                zipCode.CountryRegionId == _countryRegionId &&
                zipCode.CityRecId       == cityRecord.RecId;

            if (!zipCode.RecId)
            {
                zipCode.ZipCode         = zipCodeId;
                zipCode.City            = city;
                zipCode.CityRecId       = cityRecord.RecId;
                zipCode.CountryRegionId = _countryRegionId;
                zipCode.State           = stateId;
                zipCode.County          = countyId;
                zipCode.insert();

                numProcessedRecords++;
            }

        type = cells.item(row+1, 1).value().variantType();
    }

    while (type != COMVariantType::VT_EMPTY);

    info(strFmt("%1 Inserted",numProcessedRecords));

    ttsCommit;

    application.quit();
}

Happy DAXing.....