Generic SysTableLookup Method

It is often needed to write a custom lookup method and the SysTableLookup class can be useful to create lookups from code. However the following method uses the SysTableLookup in the background but can be called easier.

When using the SysTableLookup class, for most of the simple lookups (1 datasource table) it is alway the same. You need the following :

TableId, LookupControl, LookupFields, ReturnFields, SortFields and sometimes the use of a tmpTable.

Now following method is a generic method to user the SysTableLookup class :

public static void doLookup(TableId             _tableId,
                            Container           _lookupFields,
                            Container           _sortFields,
                            FormStringControl   _control,
                            FieldId             _returnItemFieldNum,
                            Map                 _queryRanges    = null,
                            Boolean             _useTmpTable = false,
                            Common              _tmpBuffer = null
                            )
{
    SysTableLookup          sysTableLookup  = SysTableLookup::newParameters(_tableId, _control);
    Query                   query           = new Query();
    QueryBuildDataSource    qbds;
    int                     i;
    fieldId                 lookupFieldId;
    ;

    for(i=1;i <= conlen(_lookupFields);i++)
    {
        lookupFieldId = conPeek(_lookupFields, i);

        if(lookupFieldId == _returnItemFieldNum)
            sysTableLookup.addLookupfield(lookupFieldId, true);
        else
            sysTableLookup.addLookupfield(lookupFieldId);
    }

    qbds = query.addDataSource(_tableId);

    for(i=1;i <= conlen(_sortFields);i++)
    {
        qbds.addSortField(conPeek(_sortFields, i));
    }


    if(_queryRanges)
    {
        rangeEnumerator = _queryRanges.getEnumerator();

        while (rangeEnumerator.moveNext())
        {
            qbds.addRange(rangeEnumerator.currentKey()).value(any2Str(rangeEnumerator.currentValue()));
        }
    }
    if(_useTmpTable)
        sysTableLookup.parmTmpBuffer(_tmpBuffer);

    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
}

Now when you want to create a lookup you can do it easier by doing the following :

public void lookup()
{
    Container   fieldNums       = [FieldNum(CustTable, AccountNum), FieldNum(CustTable, Name)];
    Container   sortFields      = [FieldNum(CustTable, AccountNum)];
    FieldId     returnFieldId   =  FieldNum(CustTable, AccountNum);
    Map         queryRanges     = new Map(Types::Integer, Types::String);
    ;
    queryRanges.insert(FieldNum(CustTable, AccountNum), '4000');
    LIBSysTableLookup::doLookup(TableNum(CustTable), fieldNums, sortFields, this, returnFieldId, queryRanges);
}

So the only thing you need to do is specify the fields, returnfields and sortfields…

Ans let’s look at the following example : We need a lookup with a temporary table. Then we can do it like this :

Container   fieldNums       = [FieldNum(TmpIdRef, Name), FieldNum(TmpIdRef, HelpTxt)];
    Container   sortFields      = [FieldNum(TmpIdRef, Name)];
    FieldId     returnFieldId   = ConPeek(fieldNums, 1);
    TmpIdRef    tmpTable;
    ;
    tmpTable = LIBDifferenceAction::BuildActionClassList();

    LIBSysTableLookup::doLookup(TableNum(TmpIdRef), fieldNums, sortFields, this, returnFieldId, true, tmpTable);

EventHandlers In Ax 2009

It is possible to attach event listeners to objects in X++.
The next code sample shows the usage of the SysEventBroker and SysEventInfo classes to handle events :

static void SysEventTest(Args _args)
{
SysEventBroker broker = SysEventBroker::construct();
SysEventInfo info1 = new SysEventInfo("Info-1"); // catch all;
SysEventInfo info2 = new SysEventInfo("Info-2"); // catch specific class
SysEventInfo info3 = new SysEventInfo("Info-3"); // catch specific type
;
// Setup
broker.addListener(info1, classNum(Object));
broker.addListener(info2, classNum(Runbase));
broker.addListener(info3, classNum(Object), "Fire 2"); // catch Fire 2
broker.addListener(info3, classNum(Object), "Fire 4"); // and Fire 4

broker.fireEvent(classNum(CustAutoCreate), "Fire 1", "This is my life");
broker.removeListener(info2, classNum(Runbase));
broker.fireEvent(classNum(CustAutoCreate), "Fire 2", "this is my time");
broker.fireEvent(classNum(CustAutoCreate), "Fire 3", "just show me the light");
broker.removeListener(info1);
broker.fireEvent(classNum(CustAutoCreate), "Fire 4", "and I go there");
}