Saturday, November 9, 2019

D365/AX7 – Add Dimension controls on dialog programmatically

Problem

Dimension controls are commonly used where financial impact is expected. While working on customization, we sometimes have the requirement to have these dimensions from user input and therefore to get this control dynamically render on the dialog form programmatically.

Solution

The dimension controls can be easily added to the dialog for user input using the standard helper classes and templates. Below steps can show you how to achieve this using sys operation framework classes.


Step#1
Consider we have a data contract class for sys operation class with attribute default dimension.


[DataContractAttribute,
SysOperationGroupAttribute('FinancialDimension', "@SYS101181", '1'),
SysOperationContractProcessingAttribute(classStr(MY_DimensionControlDialogUI))]
class MY_DimensionControlDialogContract implements SysOperationValidatable
{
    [DataMemberAttribute,
    SysOperationLabelAttribute("@SYS101181"),
    SysOperationGroupMemberAttribute(#FinancialDimension),
    SysOperationDisplayOrderAttribute('1')]
    public LedgerDefaultDimensionValueSet parmDefaultDimension(LedgerDefaultDimensionValueSet _defaultDimension = defaultDimension)
    {
        defaultDimension = _defaultDimension;
        return defaultDimension;
    }


}

Step#2 Let’s create a UI builder class to render the dimensions control programmatically for the above data contract.


class MY_DimensionControlDialogUI extends SysOperationAutomaticUIBuilder
{
    MY_DimensionControlDialogContract dataContract;
   
    DialogField                         dfDefaultDimension;
    DimensionEntryControl               dimensionEntryControl;
   
    #define.FinancialDimensionGroup('FinancialDimension')   

}

Step#3 In order to add the dimensions control to the dialog, override the method addDialogField in the UI builder class as below.


protected DialogField addDialogField(IdentifierName methodName, Object _dataContract = this.dataContractObject())
    {
        DialogField ret;

        if(methodName == methodStr(MY_DimensionControlDialogContract, parmDefaultDimension))
        {
            dfDefaultDimension      = DimensionEntryControlBuild::addToDialog(this.dialog(), classstr(LedgerDimensionEntryController));
        }
        else
        {
            ret = super(methodName, _dataContract);
        }
        return ret;

    }

Step#4 In order to activate and load the dimension control with default value, override the method postRun of the UI builder class.


public void postRun()
    {
        MY_LedgerDimensionHelper  dimensionHelper = new MY_LedgerDimensionHelper();
 AssetId assetId = TestAssetId’;   
       
        super();

        this.dialog().dialogForm().formRun().controlMethodOverload(false);

        //Set default dimension control
        if(dfDefaultDimension)
        {
            dimensionEntryControl = dfDefaultDimension.control();
            dimensionEntryControl.parmDisplayValues(true);
            dimensionEntryControl.parmCompany(curext());
            dimensionEntryControl.reactivate(true);
            dimensionEntryControl.loadAttributeValueSet(dimensionHelper.getAssetDefaultDimension(assetId));
        }

}


Step#5 Finally, to get the dimension value from the dimension control once user has filled the fields, override method getFromDialog as below.


public void getFromDialog()
{
DimensionDefault    dimension;

super();

if (dimensionEntryControl)
{
dimension = dimensionEntryControl.saveAttributeValueSet();
}

dataContract    = this.dataContractObject();

dataContract.parmDefaultDimension(dimension);

}

Execution

Using above mentioned steps, you can easily add the dimension control programmatically on the dialog form. On executing the dialog, the dimension control can be seen as below.

No comments:

Post a Comment