Monday, 25 November 2013

Particular financial dimension mandatory on a form in AX 2012


We may come across mandate particular financial dimension on particular form.

Here is the process to mandate Department dimension for customers.


Am using event handlers introduced in AX 2012 to achieve this.
Go through the below link to understand better on Event handlers.



1.       Create a new Class with the name ‘CustTableEventHandler’

2.       Create a new Pre-or post-event handler method as shown below


3.       Rename newly created method as ‘custValidateWrite’ and write logic as shown below.

public static void custValidateWrite(XppPrePostArgs _args)
{
    DimensionAttribute                  dimAttr;
    DimensionAttributeValue             dimAttrValue;
    DimensionAttributeValueSetItem      dimAttrValueSetItem;
    CustTable                           custTable;
    RefRecId                            defaultDimension;
    boolean                             ret;
    ;

    custTable   = _args.getThis();
    ret         = _args.getReturnValue();

    defaultDimension    =   custTable.DefaultDimension;
   
    dimAttr             =   DimensionAttribute::findByName('Department');

    select firstonly RecId, DisplayValue from dimAttrValueSetItem
                where dimAttrValueSetItem.DimensionAttributeValueSet == defaultDimension
            join dimAttrValue
                where dimAttrValue.RecId == dimAttrValueSetItem.DimensionAttributeValue &&
                dimAttrValue.DimensionAttribute == dimAttr.RecId       &&
                dimAttrValue.IsDeleted == false;
   
    if (!dimAttrValueSetItem.DisplayValue)
    {
        ret     = checkFailed("Department must be specified.");
    }

    _args.setReturnValue(ret);
}


4.       Go to ‘validateWrite’ method of CustTable then right click & select New event handler subscription as shown below.

 
5.       Go to the properties of newly created event handler method & set the properties as shown below.


 

6.       Now try to create new customer without department then it throws a message saying that ‘Department must be specified.’

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