Sunday, April 2, 2023

Lookup on form control Dynamics 365 FO using form control event handler on standard form

class MCOSAtoPOFormEventHandler

{

    [FormControlEventHandler(formControlStr(MCOSAtoPO, MCOSAtoPO_MCOSeasonCode), FormControlEventType::Lookup)]

    public static void MCOSAtoPO_MCOSeasonCode_OnLookup(FormControl sender, FormControlEventArgs e)

    {

        FormDataSource fds;

        MCOSAtoPO rTable;

        FormStringControl strControl = sender as FormStringControl ;

        fds = sender.formRun().dataSource("MCOSAtoPO");

        rTable = fds.cursor();

        Query lookupQuery = new Query();

        var selectField = [tableNum(RetailSeasonTable), fieldNum(RetailSeasonTable, seasonCode)];

        QueryBuildDataSource qbds = lookupQuery.addDataSource(tableNum(RetailSeasonTable));

        qbds.addSelectionField(fieldNum(RetailSeasonTable, seasonCode));

        qbds.addSelectionField(fieldNum(RetailSeasonTable, Description));

        qbds.addSelectionField(fieldNum(RetailSeasonTable, StartDate));

        qbds.addSelectionField(fieldNum(RetailSeasonTable, EndDate));

        var lookup =  MCOSysMultiselectionLookup::lookup(lookupQuery, selectField, strControl,strControl,strControl,strControl.valueStr());

        rTable.MCOSeasonCode = lookup.getSelectedStringsJoined();

    }

}