Reputation: 81
I have a lookup created by code filled with a query on ProjTable. I want to add a field to this lookup that is a display method on ProjTable (displayCustName) that retrieves the name from CustTable for each customer related to the relative contract of the lookup.
I found that I could do this with 'AddLookupMethod', it adds the field to the lookup but empty. I don't know how to use it properly, any help?
Thank you.
Upvotes: 1
Views: 9385
Reputation: 5107
The question you linked to in the comments basically contains the answer, but it is not stated very clearly.
In short, if you want to use addLookupMethod
with a SysTableLookup
instance, you have to make sure that you also add the fields needed for the lookup method by using addLookupField
.
Another solution would be to write your own lookup method that first retrieves the record of the table with the lookup method by using RecId
and then use the data of that record to retrieve the return value of the lookup method.
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();
}
Display method for Option 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();
}
Upvotes: 3