Monday, April 4, 2016

X++ code from inserting image for Multiple items Using Excel

static void CopyOfSK_insertdatathroughExcel(Args _args)
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    EcoResProductImageThumbnail     ecoResProductImageThumbnail;
    COMVariantType type;
    Name name;
    FileName filename;
    Sk_Exceltable  sk_Exceltable;
    BinData  bindata   = new BinData();
    container      imagecontainer;
    InventTable     inventTable;
    EcoResProduct   ecoResProduct;
    DocuRef         docuref;
    DocuOpenFile    docuOpenFile;
    EcoResProductImage ecoResProductImage;
    DocuActionArchive  docuActionArchive;
    int row;
    str Name1;
    str path;
    str filetype;
    str imagename,imagetype;
    int i;
    FileIoPermission perm;
    DocuValue docuvalue;
    #define.FileMode('W')
    ;

//specify the file path that you want to read
filename = "C:\\Users\\shivakumar.p\\Desktop\\Excel.xlsx"; //path of excel

    perm = new FileIOPermission(filename, #FileMode);
    perm.assert();
    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();
    //progress.setAnimation(#AviTransfer);
    try
    {
      ttsbegin;
        do
        {
            row++;
            if (row >= 1)
            //Name1 = any2int(cells.item(row, 1).value().toString());
            Name1     = cells.item(row,1).value().bStr();  // Itemid
            path      = cells.item(row,2).value().bStr(); //path
            filetype  = cells.item(row,3).value().bStr(); //FileType
            imagename = cells.item(row,4).value().bStr(); //Imagename
            imagetype = cells.item(row,5).value().bStr(); //imageformat

            ecoResProduct = EcoResProduct::findByDisplayProductNumber(Name1);
            inventTable   = InventTable::find(Name1);


            if(ecoResProduct)
            {
                if(inventTable)
                {
                    //if(WinAPI::pathExists(path))
                    //{

                        bindata.loadFile(path);
                        imagecontainer = bindata.getData(); //loads the image to container from path

                   // }
                    ttsBegin;

                    docuref.TypeId          = filetype;
                    docuref.RefTableId      = ecoResProduct.TableId;
                    docuref.RefRecId        = ecoResProduct.RecId;
                    docuref.RefCompanyId    = ecoResProduct.dataAreaId;
                    docuref.ActualCompanyId = curext();
                    docuref.insert();
                    docuActionArchive = DocuAction::newDocuRef(docuRef);
                    docuActionArchive.add(docuRef,path);



                    ecoResProductImage.RefRecId    = docuref.RecId;
                    ecoResProductImage.RefRecord   = docuref.RefRecId;
                    ecoResProductImage.ImageFormat = imagetype;
                    ecoResProductImage.FileName    = imagename;
                    ecoResProductImage.Usage       = Ecoresproductimageusage::External;
                    ecoResProductImageThumbnail    = new EcoResProductImageThumbnail(false);
                    ecoResProductImage.MediumSize  = ecoResProductImageThumbnail.generateThumbnail(204,204,docuref);
                    ecoResProductImage.ThumbnailSize    = ecoResProductImageThumbnail.generateThumbnail(48,48,docuRef);
                  
                    ecoResProductImage.insert();
                    ttsCommit;

                }
                else
                {
                    checkFailed("Product is not released ");
                }
            }

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


        while (type != COMVariantType::VT_EMPTY);

        ttscommit;
    }


    catch(Exception::Error)

    {

        workbooks.close();

        CodeAccessPermission::revertAssert();

        application.quit();

        ttsabort;

    }

    workbooks.close();

    CodeAccessPermission::revertAssert();

    application.quit();
}




No comments:

Post a Comment

Export a copy of the standard user acceptance testing (UAT) database

 Reference link: Export a copy of the standard user acceptance testing (UAT) database - Finance & Operations | Dynamics 365 | Microsoft ...