Particular financial dimension mandatory on a form in AX 2012 - Microsoft Dynamics 365 Vietnam

Microsoft Dynamics 365 Vietnam

Song Nghia - Microsoft Dynamics 365 Vietnam

Breaking

Monday, May 20, 2019

Particular financial dimension mandatory on a form in AX 2012

Particular financial dimension mandatory on a form in AX 2012

Nghia Song -  Microsoft Dynamics 365 Technical Consultant

Nghia Song

Tel - WhatsApp: +84967324794

Email: songnghia.uit@gmail.com

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.



Step1.  Create a new Class with the name ‘CustTableEventHandler’
Step2. Create a new Pre-or post-event handler method as shown below


step3. 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);
}

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


Step5. Go to the properties of newly created event handler method & set the properties as shown below.
Step6. Now try to create new customer without department then it throws a message saying that ‘Department must be specified.’