Tuesday, 31 January 2017

Item Default Site order setting through X++ code in ax 2009

Before that we need one Excel work book in that first column need to fill the Item numbers.
then use this job.

static void Item_Default_Site(Args _args)
{
        SysExcelApplication             application;
        SysExcelWorkbooks               workbooks;
        SysExcelWorkbook                workbook;
        SysExcelWorksheets              worksheets;
        SysExcelWorksheet               worksheet;
        SysExcelCells                   cells;
        COMVariantType                  type;
        System.DateTime                 ShlefDate;
        FilenameOpen                    filename;
        dialogField                     dialogFilename;
        Dialog                          dialog;
        //Table Declarations Starts
        DlvMode                        dlvMode;
        str ItemNum;
        InventTable             inventTable;
        InventItemInventSetup   inventItemInventSetup;
        InventItemPurchSetup    inventItemPurchSetup;
        InventItemSalesSetup    inventItemSalesSetup;
        InventDim               inventDim;

        //Table Declartions Ends
        int                             row;
        #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(typeId(FilenameOpen));
       // dialog.filenameLookupFilter(["@SYS28576″,#XLSX,"@SYS28576",#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();
        try
        {
        ttsbegin;
        do
        {
        row++;
        itemNum =(COMVariant2Str(cells.item(row,1).value()));
        #define.itemId(itemNum)
       // inventTable.ItemId = COMVariant2Str(cells.item(row,1).value());
          inventTable = InventTable::find(#itemId);
     #define.siteId('XXX')
       if(row > 1)
        {
        //Insert into InventSize Table
         if(inventTable.ItemId)
          {
             ttsBegin;

        inventDim.initValue();
        inventDim.InventSiteId = #siteId;
        inventDim = InventDim::findOrCreate(inventDim);

        //InventItemSalesSetup
        inventItemSalesSetup.clear();
        inventItemSalesSetup.initValue();
        inventItemSalesSetup.ItemId = inventTable.ItemId;
        inventItemSalesSetup.InventDimId = InventDim::inventDimIdBlank();
        inventItemSalesSetup.InventDimIdDefault = inventDim.inventDimId;
        inventItemSalesSetup.insert();

        //InventItemInventSetup
        inventItemInventSetup.clear();
        inventItemInventSetup.initValue();
        inventItemInventSetup.ItemId = inventTable.ItemId;
        inventItemInventSetup.InventDimId = InventDim::inventDimIdBlank();
        inventItemInventSetup.InventDimIdDefault = inventDim.inventDimId;
        inventItemInventSetup.insert();

        //InventItemPurchSetup
        inventItemPurchSetup.clear();
        inventItemPurchSetup.initValue();
        inventItemPurchSetup.ItemId = inventTable.ItemId;
        inventItemPurchSetup.InventDimId = InventDim::inventDimIdBlank();
        inventItemPurchSetup.InventDimIdDefault = inventDim.inventDimId;
//        inventItemPurchSetup.inventSiteId()
        inventItemPurchSetup.insert();
        ttscommit;
        }

         info(strfmt(" uploaded successfully"));
        }
        type = cells.item(row+1, 1).value().variantType();
        }while (type != COMVariantType::VT_EMPTY);
        application.quit();
        ttscommit;
        }
        catch
        {
        Error("Upload Failed");
}
}