Wednesday, 30 November 2016

Unit Conversion by code in ax 2012 R3 , X++

static void unitConversion()
{
    SysExcelApplication xlsApplication;
    SysExcelWorkBooks xlsWorkBookCollection;
    SysExcelWorkBook xlsWorkBook;
    SysExcelWorksheets xlsWorkSheetCollection;
    SysExcelWorksheet xlsWorkSheet;
    SysExcelRange xlsRange;
    SysExcelCells Cells;
    SysExcelCell RCell;
    CommaIO inFile;
    int nRow,i;
    DialogField dialogPath;
    Dialog dialog;
    Filename filename;

   

    UnitOfMeasureConversion             unitOfMeasureConversion;
    UnitOfMeasure                       unitOfMeasure;
    EcoResProduct                       ecoResProduct;
    RecId                               fromUnitofMeasure, toUnitofMeasure, _product;
    int                                 x;
    UnitIDBase                          fromunit, tounit;
    UnitOfMeasureConversionDenominator  factor;

  ;
    dialog = new Dialog("Import");
    dialogPath = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    dialog.run();
    if (dialog.run())
    {
    filename = (dialogPath.value());
    }
    inFile = new CommaIO (filename, 'R');
    if (!inFile || infile.status() != IO_Status::Ok )
    {
    throw error (strfmt("@SYS19312",filename));
    }
    try
    {
    xlsApplication = SysExcelApplication::construct();
    xlsWorkBookCollection = xlsApplication.workbooks();
    xlsWorkBookCollection.open(filename);
    xlsWorkSheetCollection = xlsApplication.worksheets();
    xlsWorkSheet = xlsWorkSheetCollection.itemFromNum(1);
    Cells = xlsWorkSheet.Cells();
    nRow = 2;
    RCell = Cells.Item(nRow, 2);
    while(RCell.value().bstr() != "")
    {
            RCell = Cells.item(nRow,1);
            itemID = RCell.value().bStr();
            RCell = Cells.item(nRow,2);
            fromUnit = RCell.value().bStr();
            RCell = Cells.item(nRow,3);
            toUnit = RCell.value().bStr();
             RCell = Cells.item(nRow,4);
            factor = RCell.value().double();

                select unitOfMeasure where unitOfMeasure.Symbol == fromunit;
                    fromunitofMeasure = unitOfMeasure.RecId;
                select unitOfMeasure where unitOfMeasure.Symbol == tounit;
                    toUnitofMeasure = unitOfMeasure.RecId;
                _product = EcoResProduct::findByProductNumber(itemID).RecId;

                    unitOfMeasureConversion.FromUnitOfMeasure   = fromunitofMeasure;
                    unitOfMeasureConversion.ToUnitOfMeasure     = toUnitofMeasure;
                    unitOfMeasureConversion.Product             = _product;
                    unitOfMeasureConversion.Factor              = factor; // pcs to box purchase factor or sales factor
                    unitOfMeasureConversion.Numerator           = 1;
                    unitOfMeasureConversion.Denominator         = 1;  // pcs to box purchase factor or sales factor opposite
                    unitOfMeasureConversion.InnerOffset         = 0.0;
                    unitOfMeasureConversion.OuterOffset         = 0.0;
                    unitOfMeasureConversion.Rounding            = UnitOfMeasureConversionRounding::Nearest;
                    unitOfMeasureConversion.insert();

         nRow++;
        RCell = Cells.Item(nRow, 2);
        }

    xlsApplication.quit ();
    xlsApplication.finalize ();
    info("Import completed");
    }
    catch( Exception::Error)
    {
    xlsApplication.quit ();
    xlsApplication.finalize ();
    ttsabort;
    info("Unable to process the excel import ");
    }

}

1 comment:

  1. Thanks for posting this informative things to people. This is really helpful for readers.Microsoft Dynamics AX consultants

    ReplyDelete