使用 AddLookupMethod

Using AddLookupMethod

我有一个由填充了 ProjTable 查询的代码创建的查找。我想在此查找中添加一个字段,该字段是 ProjTable (displayCustName) 上的显示方法,用于从 CustTable 中检索与查找的相关合同相关的每个客户的名称。

我发现我可以使用 'AddLookupMethod' 执行此操作,它将字段添加到查找中但为空。我不知道如何正确使用它,有帮助吗?

谢谢。

您在评论中链接的问题基本上包含答案,但没有说得很清楚。

简而言之,如果要将 addLookupMethodSysTableLookup 实例一起使用,则必须确保还使用 addLookupField 添加查找方法所需的字段.

另一种解决方案是编写您自己的查找方法,该方法首先通过使用 RecId 的查找方法检索 table 的记录,然后使用该记录的数据检索 return 查找方法的值。

public void lookup()
{
    SysTableLookup       sysTableLookup;
    Query                query;
    QueryBuildDataSource qbds;

    sysTableLookup = sysTableLookup::newParameters(tableNum(ProjTable), this);
    query = new Query();
    qbds = query.addDataSource(tableNum(ProjTable));
    sysTableLookup.parmQuery(query);

    sysTableLookup.addLookupfield(fieldNum(ProjTable, ProjId));
    /* option 1: add the CustAccount field to the lookup so it can be used by the custName method
    sysTableLookup.addLookupfield(fieldNum(ProjTable, CustAccount));
    sysTableLookup.addLookupMethod(tableMethodStr(ProjTable, custName));
    */
    // option 2: write your own lookup method
    sysTableLookup.addLookupMethod(tableMethodStr(ProjTable, myCustName));

    sysTableLookup.performFormLookup();
}

选项 2 的显示方法:

/// <summary>
///    Retrieves the customer name.
/// </summary>
/// <returns>
///    The name of the customer.
/// </returns>
display CustName myCustName()
{
    ProjTable projTable;

    // the ProjTable record referenced by 'this' will not contain the field CustAccount
    // if this method is called by a SysTableLookup instance that does not explicitely 
    // contain CustAccount as lookup field
    // therefore the ProjTable record must be reselected using RecId
    select firstOnly CustAccount from projTable where projTable.RecId == this.RecId;
    return CustTable::find(projTable.CustAccount).name();
}