Monday 10 April 2017

Multiple Custom Lookups Using SysTableLookup On Dialog Class And Filtering Second Lookup using First Lookup value

// Create Dialog Class and extend it with RunBase

class CustomDialog extends RunBase
{

    CustTable           custTable;
    SalesLine           salesLine;
    SalesTable          salesTable;
    CustAccount         custAccount;
    SalesId             salesId;

    Dialog              dialog;

    DialogField         fieldAccount, SalesOrders, Items;
    FormStringControl   control1, control2, control3;

}

//Create an Override Method Dialog

protected Object Dialog()
{

    dialog = super();

    // Set a title for dialog
    dialog.caption( 'Simple Dialog');

    // Add a new field to Dialog
    fieldAccount = dialog.addField( extendedTypeStr(AccountNum), 'Customer account' );
    control1     = fieldAccount.control();

    SalesOrders  = dialog.addField(extendedTypeStr(salesId), "Sales Orders");
    control2     = SalesOrders.control();
//
    Items        = dialog.addField(extendedTypeStr(itemId), "Items");
    control3     = items.control();

    control1.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(CustCreateDialog, Accountlookup), this);

    control2.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(CustCreateDialog, OrdersLookup), this);
    control3.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(CustCreateDialog, ItemsLookup), this);

    return dialog;
}

// Create Lookup For FieldAccount Dialog Field

private void AccountLookup(FormStringControl    _control)
{
    SysTableLookup          sysTableLookup = new SysTableLookup() ;


    QueryBuildDataSource    qbds1;


    Query                   query = new Query();

    qbds1 = query.addDataSource(tablenum(SalesLine));

    sysTableLookup = SysTableLookup::newParameters(tablenum(SalesLine), _control );
    sysTableLookup.addLookupfield(fieldnum(SalesLine, CustAccount));

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

// Create Lookup For SalesOrders Dialog Field

private void OrdersLookup(FormStringControl _control)
{
    SysTableLookup          sysTableLookup = new SysTableLookup() ;

    QueryBuildDataSource    qbds1;
    QueryBuildRange         range;

    Query                   query = new Query();

    qbds1 = query.addDataSource(tablenum(SalesLine));

    range = qbds1.addRange(fieldNum(SalesLine, CustAccount));
    range.value(queryValue(fieldAccount.value()));
    sysTableLookup = SysTableLookup::newParameters(tablenum(SalesLine),_control);
    sysTableLookup.addLookupfield(fieldnum(SalesLine, salesId));

    SysTableLookup.parmQuery(query);

    SysTableLookup.performFormLookup();

}

// Create Lookup For Items Dialog Field

private void itemsLookup(FormStringControl  _control)
{
    SysTableLookup          sysTableLookup = new SysTableLookup() ;

    QueryBuildDataSource    qbds1;
    QueryBuildRange         range;
    Query                   query = new Query();

    qbds1 = query.addDataSource(tablenum(SalesLine));
    range = qbds1.addRange(fieldNum(salesTable, salesId));
    range.value(queryValue(SalesOrders.value()));
    sysTableLookup = SysTableLookup::newParameters(tablenum(salesLine),_control);
    sysTableLookup.addLookupfield(fieldnum(SalesLine, ItemId));

    SysTableLookup.parmQuery(query);
    SysTableLookup.performFormLookup();

}

//Create Run method if you want to execute any logic

public void run()
{
    // Set Dialog field value to find SalesLine
    salesLine = salesLine::find("000003");

    if (salesLine)
    {
        // Shows retrieved information
        info( strFmt('%1 -- %2' , salesLine.custaccount, salesLine.salesid));
    }
    else
    {
        error( 'Customer Account not found!');
    }

}

//Create Main()

public static void main(Args _args)
{
    CustomDialog custCreate = new CustomDialog();

    // Prompt the dialog, if user clicks in OK it returns true
    if (custCreate.prompt())
    {
        custCreate.run();
    }
}

No comments:

Post a Comment