Monday, 15 June 2015

Import Unit Of Conversions Data - Ax 2012

Hello Guys...!

Here I'm sharing code for importing Unit Of Conversions in Ax 2012.


Step 1: Copy and Paste below code  in Job (AOT->Job)


 static void Rahul_UnifOfConversion_Import(Args _args)
{
    CommaIO                     csvFile;
    container                   readCon;
    counter                     icount,inserted;
    Dialog                      dialog;
    DialogField                 dfFileName;
    FileName                    fileName;
    str                         transDate;
    str                         _fromUnitOfMeasure, _toUnitOfMeasure, _product;
    real                        _factor, _innerOffset, _outerOffset;
    int                         _numerator,_denominator;
    str                         _rounding;
    UnitOfMeasureConversion     unitOfMeasureConversion, UnitOfMeasureConversionLocal;
    UnitOfMeasureConversionRounding UnitOfMeasureConversionRounding;
    inserted =0;
    //
    #File
    dialog     = new Dialog("Pick the file");
    dfFileName = dialog.addField(extendedTypeStr(FileNameOpen));
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    if(dialog.run())
      {
         filename =  dfFileName.value();
      }
         csvFile  =  new CommaIO(filename,'r');
    if(csvFile)
      {
        ttsBegin;
        while (csvFile.status() == IO_Status::OK)
        {
          readCon  = csvFile.read();
          if(readCon)
            {
                _fromUnitOfMeasure = conPeek(readCon,1);
                _toUnitOfMeasure = conPeek(readCon,2);
                _product = conPeek(readCon,3);
                _factor = conPeek(readCon,4);
                _numerator = conPeek(readCon,5);
                _denominator = conPeek(readCon,6);
                _innerOffset = conPeek(readCon,7);
                _outerOffset = conPeek(readCon,8);
                _rounding = conPeek(readCon,9);
                unitOfMeasureConversion.FromUnitOfMeasure   = UnitOfMeasure::findBySymbol(_fromUnitOfMeasure).RecId;
                unitOfMeasureConversion.ToUnitOfMeasure     = UnitOfMeasure::findBySymbol(_toUnitOfMeasure).RecId;
                unitOfMeasureConversion.Product             = EcoResProduct::findByDisplayProductNumber(_product).RecId;
                unitOfMeasureConversion.Factor              = _factor;
                unitOfMeasureConversion.Numerator           = _numerator;
                unitOfMeasureConversion.Denominator         = _denominator;
                unitOfMeasureConversion.InnerOffset         = _innerOffset;
                unitOfMeasureConversion.OuterOffset         = _outerOffset;
                unitOfMeasureConversion.Rounding            =  str2enum(UnitOfMeasureConversionRounding ,_rounding);
                UnitOfMeasureConversionLocal.RecId = UnitOfMeasureConversion::findByConversion(unitOfMeasureConversion.FromUnitOfMeasure,
                                                                                         unitOfMeasureConversion.ToUnitOfMeasure).RecId;
                if(UnitOfMeasureConversionLocal.RecId == 0)
                {
                    unitOfMeasureConversion.insert();
                    inserted++;
                    icount++;
                }
               }
            }
          ttsCommit;
      }
    info(strFmt("%1 records has been imported out of %2", inserted, icount));
}

Step 2 : Fill data in excel sheet in the below order and save in csv format.(Delete Header lines)

                 fromUnitOfMeasure
                _toUnitOfMeasure
                _product
                _factor
                _numerator
                _denominator
                _innerOffset
                _outerOffset
                _rounding
Step 3 :Run the job and browse csv file and press ok.. That's it


@Rahul Talasila

No comments:

Post a Comment

Sorting data in a form datasource in Dynamics Ax 2012 / D365F&O X++

In Microsoft Dynamics AX, you can sort a form datasource when the form is loaded. On the form, override the init method. After the call...