Skip to main content

Notifications

Update Wrong item Name Using Excel File in AX 2012

if you are facing wrong item name and you have  a lot of them you can use excel file with the wrong and the correct Item Name then you can use the following code to help with the update process :

Excel sheet format is :

item code

wrong item name

correct item name 

void clicked()

{

//-->excel definations//////////////////////////

 SysExcelApplication             application;

    SysExcelWorkbooks               workbooks;

    SysExcelWorkbook                workbook;

    SysExcelWorksheets              worksheets;

    SysExcelWorksheet               worksheet;

    SysExcelCells                   cells;

    COMVariantType                  type;

    System.DateTime                 ShlefDate;

    FilenameOpen                    filename;

    dialogField                     dialogFilename;

    Dialog                          dialog;

 /////////////////////////////////////////////////////////

 //--> table definations///////////////////////////////

InventTable _inventTable;

EcoResProductTranslation ecoResProductTranslation;

    companyinfo _ci;

////////////////////////////////////////////////////////

//--> other variables /////////////////////////////////////

int row=0;

int counter=0;

ItemName itemName;

str itemDesc;

/////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////

  #Excel

    // convert into str from excel cell value

    str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)

    {

    switch (_cv.variantType())

    {

    case (COMVariantType::VT_BSTR):

    return _cv.bStr();

    case (COMVariantType::VT_R4):

    return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);

    case (COMVariantType::VT_R8):

    return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);

    case (COMVariantType::VT_DECIMAL):

    return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);

    case (COMVariantType::VT_DATE):

    return date2str(_cv.date(),123,2,1,2,1,4);

    case (COMVariantType::VT_EMPTY):

    return "";

    default:

    throw error(strfmt("@SYS26908", _cv.variantType()));

    }

    return "";

    }

    ;

    dialog              =   new Dialog("Excel Upoad");

    dialogFilename      =   dialog.addField(extendedTypeStr(FilenameOpen));

    dialog.filenameLookupFilter(["@TRA60",#XLSX,"@TRA60",#XLS]);

    dialog.filenameLookupTitle("Upload from Excel");

    dialog.caption("Excel Upload");

    dialogFilename.value(filename);

    if(!dialog.run())

    return;

    filename            =   dialogFilename.value();

    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();

 ttsbegin;

 do

   {

    try

      {

          row++;

                  itemId              = COMVariant2Str(cells.item(row, 1).value());

                  itemName           = COMVariant2Str(cells.item(row, 2).value());

                  itemDesc            = COMVariant2Str(cells.item(row, 3).value());

        if(row > 1 )

         {

             while select forupdate ecoResProductTranslation where ecoResProductTranslation.Name==itemName

             {

               ecoResProductTranslation.Name= itemDesc;

               ecoResProductTranslation.doUpdate();

                 counter++;

             }

         }

     }

  catch

     {

         Error(strfmt("Upload Failed in row %1", row));

     }

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

    }while (type != COMVariantType::VT_EMPTY);

    ttscommit;

    info(strfmt(" updated items : %1 ",counter));

    application.quit();

   // super();

}

My Technical Blog :

My Linked In :

Comments

*This post is locked for comments