Friday, October 2, 2015

How to lookup and set a new value for Financial dimension

Let's say we need to change a value for one of item financial dimension.

I created a simple form with the item list and their default financial dimensions that are controlled by a standard controller.

There are also two unbound controls that allows to select any related financial dimension attribute and its available value. It is done by means of two edit methods and one lookup method, which can be added to your class and used everywhere you need.

A new chosen value can be set for the item financial dimension by the Set new value button. Actually it uses a method that, again, can be added to your class a static one.

I would like to thank Carsten Glem for his comment on this topic.

Here comes the code for the main methods. Feel free also to download the whole project.

Form class declaration
public class FormRun extends ObjectRun
    // Standard controller placing predefined financial attributes on a form tab page (FinDimTab in this example)
    DimensionDefaultingController   dimensionDefaultingController;
    // New financial dimension attribute; placed on the form as an unbound control through its name and edit method
    // via editAttributeToChangeName
    DimensionAttributeRecId         attributeToChange;
    Name                            attributeToChangeName;
    // New value for a chosen financial dimension attribute; placed as an unbound control through edit method
    // via editAttributeToChangeValue
    DimensionValue                  attributeToChangeValue;

    // each time user change the attribute name, it changes attribute, if valid name, and clears its value on the form
    // the lookup method LookupDimensionValueByAttribute can be placed in a class as a static to be used everywhere
    // Set new value button calls SetDefaultDimensionValue method, 
    // which can actually be added to a class in a class as a static to be used everywhere


/// <summary>
/// Creates and displays a lookup from which the user can select for parent category.
/// </summary>
/// <param name="_formReferenceControl">
/// The calling form reference control.
/// </param>
/// <param name="_parentCategory">
/// The RecId of parent category
/// </param>
/// <returns>
/// The selected record in the case of record mode selection; otherwise null.
/// </returns>
public void LookupDimensionValueByAttribute(FormStringControl _formControl, str _dimensionAttributeValueStr, DimensionAttribute _dimensionAttribute)
    Args                    args;
    FormRun                 lookupFormRun;
    Object                  object;

    if (_formControl != null && _dimensionAttribute)
        // Construct arguments for the custom lookup
        args = new Args();;


        // Run the custom lookup and init the lookup form
        lookupFormRun = classfactory.formRunClass(args);

        // Specify this is the callback on the lookup form by casting the
        // form to an object and late binding the setResultCallback method
        if (SysFormRun::hasMethod(lookupFormRun, identifierStr(setResultCallback)))
            object = lookupFormRun;



/// <summary>
/// Sets a value in a default dimension field of a given record, i.e. InventTable, CustTable etc.
/// </summary>
/// <param name="_fromDefaultDimension">
/// <C>DimensionDefault</C> field from the existing record, i.e. InventTable.DefaultDimension.
/// </param>
/// <param name="_attributeName">
/// The name of the dimension attribute to set the value for.
/// </param>
/// <param name="_newDimAttributeValue">
/// The new value for the given dimension attribute.
/// </param>
/// <returns>
/// The new default dimension.
/// </returns>
/// Note: taken from
public DimensionDefault SetDefaultDimensionValue(   DimensionDefault    _fromDefaultDimension,
                                                    Name                _attributeName,
                                                    Str                 _newDimAttributeValue)
    DimensionAttributeValueSetStorage   dimensionAttributeValueSetStorage   = new DimensionAttributeValueSetStorage();
    DimensionAttributeValue             dimensionAttributeValue;
    DimensionDefault                    toDefaultDimension;
    Map                                 dimensionSpecifiers                 = DimensionDefaultingEngine::getDefaultDimensionSpecifiers(_fromDefaultDimension);
    MapEnumerator                       enumerator;
    DimensionAttribute                  dimensionAttribute;

    //Add current dimension attribute values
    enumerator = dimensionSpecifiers.getEnumerator();
    while (enumerator.moveNext())
        dimensionAttribute      = DimensionAttribute::find(enumerator.currentKey());
        dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(DimensionAttribute::findByName(dimensionAttribute.Name), enumerator.currentValue());

    //Add the extra dimension attribute values
    dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(DimensionAttribute::findByName(_attributeName), _newDimAttributeValue, true, true);
    dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue); // Replaces since attribute already exists with different value
    toDefaultDimension      =;

    return toDefaultDimension;

No comments: