Friday, July 25, 2014

Custom lookup based on fields in Form

Sample situation: Payment category field need to filter based on Sales Order sales type (SalesTable.AvSalesType).
 If sales type == sales contract then filter payment category where AvTurnIn == Yes.
If sales type == service invoice then filter payment category where AvServiceInvoice == Yes.

Code:
Override AvPayment.AvPaymentCategory.lookup().

public void lookup(FormControl _formControl, str _filterStr)
{
    Query query = new Query();
    QueryBuildDataSource queryBuildDataSource;
    QueryBuildRange queryBuildRange;
    SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(AvPaymentCategory), _formControl);
    sysTableLookup.addLookupField(fieldNum(AvPaymentCategory, AvPaymentCateogry));
    sysTableLookup.addLookupField(fieldNum(AvPaymentCategory, Description));
    queryBuildDataSource = query.addDataSource(tableNum(AvPaymentCategory));

    if (SalesTable.AvSalesType == AvSalesType::SalesContract)
    {
        queryBuildRange = queryBuildDataSource.addRange(fieldNum(AvPaymentCategory, AvTurnIn));
    }
    else
    {
        queryBuildRange = queryBuildDataSource.addRange(fieldNum(AvPaymentCategory, AvServiceInvoice));
    }

    queryBuildRange.value('1');

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

}