Wednesday, May 26, 2010

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();
 
    XYZSysTableLookup::doLookup(TableNum(TmpIdRef), fieldNums, sortFields, this, returnFieldId, true, tmpTable);

No comments: