Thursday, March 19, 2015

Financial Dimension value in lookup

At times you'll be asked to bring the dimension values in lookup, which might be pretty difficult task.

Here in this post i have created a class using which you could easily bring the financial dimension values in lookup 

AX 2012
class DImensionLookupByName_Sa
{
}

-->>then create a method 

Public static client void lookupDimension(FormStringControl stringControl, Name dimensionName)
{
    Args                    args;
    FormRun                 lookupFormRun;
    DimensionAttribute      dimAttribute;

    if (_stringControl != null)
    {
        args = new Args();
        args.name(formStr(DimensionDefaultingLookup));
        args.lookupValue(_stringControl.text());
        args.caller(_stringControl);        
        dimAttribute = DimensionAttribute::findByName(_dimensionName);
        args.lookupField(dimAttribute.ValueAttribute);
        args.record(dimAttribute);        
        lookupFormRun = classfactory.formRunClass(args);
        lookupFormRun.init();
        _stringControl.performFormLookup(lookupFormRun);
    }
}



Here the two parameters that you are supposed to pass is the form control and the dimension's name which you wanna bring in the lookup..(say businessunit,costcenter,customcostcenter and so on.. )

so, 

create a form with a stringedit control and then , create the one line code in the lookup method 

DImensionLookupByName_Sa::lookupDimension(this,"Businessunit");

you will be able to see the dimension values of businessunit in the lookup.. 

or else, 

You could create a form with two stingedit controls say stringedit1 and stringedit2.

and .. in the stringedit1 use this in the lookup method.. 

{
   Query           query;

SysTableLookup  sysTableLookup;

super();

sysTableLookup = SysTableLookup::newParameters(tableNum(DimensionAttribute), this);

sysTableLookup.addLookupfield(fieldNum(DimensionAttribute, Name));

query = new Query();

query.addDataSource(tableNum(DimensionAttribute));

sysTableLookup.parmQuery(query);

sysTableLookup.performFormLookup();
}



This would bring all the dimensionattributes name in the lookup 

and then in the second field's lookup enter the following code

DImensionLookupByName_Sa::lookupDimension(this,stringedit1.text());

this would bring the values of the dimension selected in stringedit1.

Alternate Logic in D365FO X++:

public static void DimensionValueLookup(
        FormStringControl   _dimensionValueControl,
        Name                _localizedName,
        boolean             _promptErrorMessage = false)
    {
        Query                   query = new Query();
        SysTableLookup          sysTableLookup;
        QueryBuildDataSource    qbds;

        DimensionAttribute      dimensionAttribute;
        RecId                   dimensionAttributeId;
        DataAreaId              dataAreaId = curext();

        if (_localizedName)
        {
            dimensionAttribute      = DimensionAttribute::findByLocalizedName(_localizedName, false, currentUserLanguage());
            dimensionAttributeId    = dimensionAttribute.RecId;
        }

        if (dimensionAttributeId)
        {
            sysTableLookup = SysTableLookup::newParameters(DimensionCache::instance().dimensionAttributeBackingTable(dimensionAttributeId), _dimensionValueControl);

            sysTableLookup.addLookupfield(dimensionAttribute.ValueAttribute);
            LedgerDimensionTranslationLookupHelper::addLookupTranslation(sysTableLookup, dimensionAttributeId);
            sysTableLookup.addSelectionField(dimensionAttribute.NameAttribute);

            changeCompany(dataAreaId)
            {
                qbds = query.addDataSource(DimensionCache::instance().dimensionAttributeBackingTable(dimensionAttributeId));
                qbds.addOrderByField(DimensionCache::instance().dimensionAttributeValueField(dimensionAttributeId));
                DimensionAttribute::restrictQueryToCategorizedValues(qbds, dimensionAttributeId);
            }

            sysTableLookup.parmQuery(query);
            sysTableLookup.performFormLookup();
        }
        else if (_promptErrorMessage)
        {
            //Please choose a value for "Dimension type" first!
            checkFailed("@GLS100015");
        }
    }


or

public static void lookupDimensionValues(DimensionAttribute _dimensionAttribute, FormStringControl _formStringControl, CompanyId _companyId = curext(), DimensionLookupParameters _lookupParameters = null)
    {
        Args        args;
        FormRun     formRun;

        Debug::assert(_formStringControl != null);
        Debug::assert(_companyId != '');

        if (!_lookupParameters)
        {
            _lookupParameters = new DimensionLookupParameters();
        }

        if (_lookupParameters.parmFilterDate() == dateNull())
        {
            _lookupParameters.parmFilterDate(DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone()));
        }

        changecompany(_companyId)
        {
            args = new Args();
            args.name(formStr(DimensionLookup));
            args.callerFormControl(_formStringControl);
            args.caller(_formStringControl.formRun());
            args.lookupValue(_formStringControl.text());

            args.lookupField(_dimensionAttribute.ValueAttribute);
            args.record(_dimensionAttribute);
            args.parmObject(_lookupParameters);

            formRun = classfactory.formRunClass(args);
            formRun.init();

            _formStringControl.performFormLookup(formRun);
        }
    }

No comments:

Post a Comment

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...