Monday, 25 November 2013

Disable financial dimensions in AX 2012 through code



Disabling particular financial dimension based on record or on particular form quite tricky in AX 2012 compared to AX 2009.

Here is an example to achieve disabling Business unit financial dimension on Customers form in AX 2012.

Process to disable Business Unit financial dimension on Customers form.
1.       Create a new parm method in ‘DimensionDefaultingControllerBase’ class as shown below  

void parmEditableDimensionAttributeSetId(RefRecId _editableDimensionAttributeSetId  = editableDimensionAttributeSetId)
{
    editableDimensionAttributeSetId = _editableDimensionAttributeSetId;
}

2.       Create a new method in ‘CustTable’ form as shown below  

private void setDimensionAttributeSetStorage()
{
    DimensionAttribute              dimAttr;
    DimensionAttributeSetItem       dimAttrSetItem;
    DimensionEnumeration            dimensionSetId =  DimensionCache::getDimensionAttributeSetForLedger();
    DimensionAttributeSetStorage    dimensionAttributeSetStorage;
    ;

    dimensionAttributeSetStorage = new DimensionAttributeSetStorage();

    while select * from dimAttr
            order by Name
            where dimAttr.Type != DimensionAttributeType::MainAccount
        join RecId from dimAttrSetItem
            where dimAttrSetItem.DimensionAttribute == dimAttr.RecId &&
                dimAttrSetItem.DimensionAttributeSet == dimensionSetId
    {
        if (dimAttr.Name != 'BusinessUnit') // Except BusinessUnit rest should enable
            dimensionAttributeSetStorage.addItem(dimAttr.RecId, dimAttr.HashKey,NoYes::Yes);
    }

      dimensionDefaultingController.parmEditableDimensionAttributeSetId(dimensionAttributeSetStorage.save());
}

3.       Add below highlighted code in ‘init’ method of ‘CustTable’ form as shown below     
Calling newly created method before dimensionDefaultingController.pageActivated();

dimensionDefaultingController = DimensionDefaultingController::constructInTabWithValues(true, true, true, 0, this, tabFinancialDimensions, "@SYS138487");
    dimensionDefaultingController.parmAttributeValueSetDataSource(custTable_ds, fieldStr(CustTable, DefaultDimension));
    // Added by Shankar on 25/11/2013 DisableFinDimensions - Begin
    this.setDimensionAttributeSetStorage();
    // Added by Shankar on 25/11/2013 DisableFinDimensions - End
    dimensionDefaultingController.pageActivated();

4.       Now open Customers form & check financial dimensions tab it shows as below.
Can see Business Unit financial dimension disabled, highlighted one.

 

Hope this topic would be helpful...Enjoy... 

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...