Tuesday, December 1, 2015

Importing Unit of Measures of AX 2009 to 2012 using EXCEL.

UNIT_Excel (Importing Units of 2009)
==============================
static void Unit_Excel(Args _args)
{
    Dialog dialog;
    DialogField dialogfield;
    Filename filename;

    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;

    COMVariantType type;
    int row;
    UnitOfMeasure unitofmeasure,UnitFM;

    ;

    dialog = new dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(FilenameOpen), 'File Name');
    dialog.run();

    if(dialog.run())
    {
     filename =(dialogfield.value());
    }

    row=1;
    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
    {
    row++;
    unitofmeasure.DecimalPrecision=any2int(cells.item(row,1).value().double());
    unitofmeasure.Symbol=cells.item(row,2).value().bStr();
    unitofmeasure.SystemOfUnits=any2int(cells.item(row,3).value().double());
    unitofmeasure.UnitOfMeasureClass=any2int(cells.item(row,4).value().double());
    UnitFM=unitofmeasure::findBySymbol(unitofmeasure.Symbol,true);
        if(UnitFM)
    {
        ttsBegin;
        UnitFM.selectForUpdate(true);
        UnitFM.update();
        ttsCommit;
    }
    else
        unitofmeasure.insert();

    info(strfmt('%1 ', unitofmeasure.RecId ));
    type = cells.item(row+1, 1).value().variantType();

    }
    while (type != COMVariantType::VT_EMPTY);
    application.quit();

}

-===================================================================
UOM_Excel ( Importing Unit of Measures of 2009)
====================================================================

static void UOM_Excel(Args _args)
{
    Dialog dialog;
    DialogField dialogfield;
    Filename filename;

    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;

    COMVariantType type;
    int row,cnt=0;
    UnitOfMeasureConversion unitOfMeasureConversion, unitofmeasure;
    ;

    dialog = new dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(FilenameOpen), 'File Name');
    dialog.run();

    if(dialog.run())
    {
     filename =(dialogfield.value());
    }

    row=1;
    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
    {
        row++;
        unitOfMeasureConversion.Product             = EcoResProduct::findByProductNumber(cells.item(row, 1).value().bStr()).Recid;
        unitOfMeasureConversion.ToUnitOfMeasure     = UnitOfMeasure::findBySymbol(cells.item(row, 4).value().bStr()).RecId;

        unitOfMeasureConversion.Factor              = cells.item(row, 3).value().double();

        unitOfMeasureConversion.Numerator           = 1;
        unitOfMeasureConversion.Denominator         = 1;
        unitOfMeasureConversion.FromUnitOfMeasure   = UnitOfMeasure::findBySymbol(cells.item(row, 2).value().bStr()).RecId;
        unitOfMeasureConversion.Rounding            = UnitofMeasureconversionrounding::Nearest;//cells.item(row, 5).value().bStr();
        unitofmeasure = UnitOfMeasureConversion::findByConversion(unitOfMeasureConversion.FromUnitOfMeasure, unitOfMeasureConversion.ToUnitOfMeasure,unitOfMeasureConversion.Product);
        if( unitOfMeasureConversion.ToUnitOfMeasure!=0 &&  unitOfMeasureConversion.FromUnitOfMeasure!=0)
         {
          if(unitofmeasure)
          {
            ttsBegin;
            unitofmeasure.selectForUpdate(true);
            unitofmeasure.update();
            ttsCommit;
          }
          else
             {
            unitOfMeasureConversion.insert();
                 cnt++;
             }
          info(strfmt(' Conversions imported for the products=%1 ', EcoResProduct::findByProductNumber(cells.item(row, 1).value().bStr()).DisplayProductNumber ));

         }
        else
        {
            info(strfmt('Conversions are not imported for products=%1 ',  EcoResProduct::findByProductNumber(cells.item(row, 1).value().bStr()).DisplayProductNumber ));

        }
         type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    info(strFmt('No Of Conversions imported are=%1',cnt));
    application.quit();


}

--------------------------------------------------------------------------------------------------------------------------

Below is the method to check the Text format


static void UOM_Excel(Args _args)
{
    Dialog                         dialog;
    DialogField                 dialogfield;
    Filename                    filename;

    SysExcelApplication  application;
    SysExcelWorkbooks  workbooks;
    SysExcelWorkbook    workbook;
    SysExcelWorksheets  worksheets;
    SysExcelWorksheet   worksheet;
    SysExcelCells              cells;
 
    EcoResProduct          ecoResProduct;
    InventTable                inventtable;
    ItemId                        itemid;
 
    COMVariantType      type;
    int                              row,cnt=0;
    UnitOfMeasureConversion unitOfMeasureConversion, unitofmeasure;
    ;

    dialog = new dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(FilenameOpen), 'File Name');
    dialog.run();

    if(dialog.run())
    {
     filename =(dialogfield.value());
    }

    row=1;
    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
    {
        row++;
     
     
        switch(cells.item(row, 1).value().variantType())
        {
        case COMVariantType::VT_BSTR:
            itemid = strFmt("%1", cells.item(row, 1).value().bStr());
            break;
        case COMVariantType::VT_DECIMAL, COMVariantType::VT_R4, COMVariantType::VT_R8:
            itemid = strFmt("%1", any2int(cells.item(row, 1).value().double()));
            break;
        case COMVariantType::VT_I1, COMVariantType::VT_I2, COMVariantType::VT_I4:
            itemid = strFmt("%1", cells.item(row, 1).value().int());
            break;
        case COMVariantType::VT_UI1, COMVariantType::VT_UI2, COMVariantType::VT_UI4:
            itemid = strFmt("%1", cells.item(row, 1).value().uLong());
            break;
         case COMVariantType::VT_EMPTY:
            itemid = '';
            break;
        default:
            throw error(strfmt('Unhandled variant type (%1).', cells.item(row, 1).value().variantType()));
        }

     
     
     
        if(itemid)
        {
         
        inventtable = InventTable::find(itemid);  
         
        unitOfMeasureConversion.Product             = inventtable.Product;
        unitOfMeasureConversion.ToUnitOfMeasure     = UnitOfMeasure::findBySymbol(cells.item(row, 4).value().bStr()).RecId;

        unitOfMeasureConversion.Factor              = cells.item(row, 3).value().double();

        unitOfMeasureConversion.Numerator           = 1;
        unitOfMeasureConversion.Denominator         = 1;
        unitOfMeasureConversion.FromUnitOfMeasure   = UnitOfMeasure::findBySymbol(cells.item(row, 2).value().bStr()).RecId;
        unitOfMeasureConversion.Rounding            = UnitofMeasureconversionrounding::Nearest;        unitofmeasure = UnitOfMeasureConversion::findByConversion(unitOfMeasureConversion.FromUnitOfMeasure, unitOfMeasureConversion.ToUnitOfMeasure,unitOfMeasureConversion.Product);
     
        if( unitOfMeasureConversion.ToUnitOfMeasure!=0 &&  unitOfMeasureConversion.FromUnitOfMeasure!=0)
         {
          if(unitofmeasure)
          {
            ttsBegin;
            unitofmeasure.selectForUpdate(true);
            unitofmeasure.update();
            ttsCommit;
          }
          else
             {
            unitOfMeasureConversion.insert();
                 cnt++;
             }
          info(strfmt(' Conversions imported for the products=%1 ', inventtable.ItemId));

         }
        else
        {
            info(strfmt('Conversions are not imported for products=%1 ',  inventtable.ItemId));

        }
        }
        else
        {
            info(strfmt('Item not found: %1',cells.item(row, 1).value().bStr()));
        }
         type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    info(strFmt('No Of Conversions imported are=%1',cnt));
    application.quit();


}


No comments:

Post a Comment

How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// &l...