Tuesday 27 August 2013

Importing Workers Image using Excel in AX 2012

Dear Friends,

Below is sample code to import Workers Image.

 void importHCMPersonsImage()
    {
        HcmPersonImage      hcmPersonImage;
        HcmWorker           hcmWorker;
        HcmPersonnelNumberId  personnelNumber;
        DirPartyRecId       person;
        container           imageContainer;
        COMVariant          COMVariant1;
        SysExcelApplication app;
        SysExcelWorkbooks   Workbooks;
        SysExcelWorkbook    Workbook;
        SysExcelWorksheets  Worksheets;
        SysExcelWorksheet   Worksheet;
        SysExcelCells       Cells;
        SysExcelCell        RCell;
        InterOpPermission   IOPermission =  new 

                                                InterOpPermission(InterOpKind::ComInterop);
        int                 line, success, failed;
        Line                lastLineNum;
        Dialog              dialog;
        FileNameOpen        fileNameOpen;
        DialogField         dlgFileName;
        FilenameFilter filter = ["@SYS28576", '*.xlsx', "@SYS27373", '*.*'];
        FreeTxt             txt;
        BinData binData = new BinData();
        str extention, path, nameOfFile;
        str                 imageFilePathName;
        excel
        ;
  
        dialog = new Dialog();
  
        dialog.caption("@SYS4820");
        dlgFileName   =   dialog.addField(extendedTypeStr(FilenameOpen));
  
        dialog.filenameLookupFilter(filter);
  
        if (dialog.run())
        {
            fileNameOpen    =   dlgFileName.value();
        }
  
        IOPermission.assert();
  
        app = SysExcelApplication::construct();
        Workbooks = app.Workbooks();
        COMVariant1 = new COMVariant();
        COMVariant1.bStr(fileNameOpen);
        Workbook = Workbooks.Add(COMVariant1);
        Worksheets = Workbook.worksheets();
        Worksheet = Worksheets.itemFromName('Images');
        Cells = Worksheet.Cells();
  
        line = 2;
  
        setprefix('Import summary');
  
        startLengthyOperation();
        try
        {
  
            ttsbegin;
            RCell = Cells.Item(line, 1);
  
            while(RCell.Value().bStr())
            {
                personnelNumber = RCell.value().bStr();
                hcmWorker   = 

                                 HcmWorker::findByPersonnelNumber(personnelNumber);
                if (!hcmWorker.RecId)
                {
                    line++;
                    RCell = Cells.Item(line, 1);
                    info("@SYS9367" + int2str(line) +"@SYS35675"+ 

                           strfmt("@SYS112224", personnelNumber,  
                           fieldpname(HcmWorker, personnelNumber),    
                           tablepname(HcmWorker))+'\n');

                    continue;
                }
  
                person      = hcmWorker.Person;
  
                RCell = Cells.item(line, 2);
                imageFilePathName = RCell.value().bStr();
  
                if (!WinAPI::fileExists(imageFilePathName))
                {
                    line++;
                    RCell = Cells.Item(line, 1);
                    info("@SYS9367" + int2str(line) +"@SYS35675"+ strfmt("File 

                           name %1 does not exists.", imageFilePathName)+'\n');
                    continue;
                }
  
                [path, nameOfFile, extention] = fileNameSplit(imageFilePathName);
                if (extention == '.bmp' ||
                    extention == '.jpg' ||
                    extention == '.gif' ||
                    extention == '.jpeg')
                {
                    binData.loadFile(imageFilePathName);
                    imageContainer = binData.getData();
                }
  
                hcmPersonImage = HcmPersonImage::findByPerson(person, true);
  
                if(!hcmPersonImage.RecId)
                {
                    hcmPersonImage.Person = person;
                    hcmPersonImage.Image  = imageContainer;
                    hcmPersonImage.insert();
                }
                else
                {
                    hcmPersonImage.Image = imageContainer;
                    hcmPersonImage.update();
                }
  
                line++;
                RCell = Cells.Item(line, 1);
  
  
            }
  
            ttscommit;
        }
        catch
        {
            txt = 'Error in line '+ int2str(line)+", "+'Process aborted.';
            throw error(txt);
        }
        endLengthyOperation();
  
        CodeAccessPermission::revertAssert();
    }


And below is the excel template used...

 

 

No comments:

Post a Comment