Thursday, June 30, 2022

Multi select Lookup method for string fields D365 FO

Both the code is written on form datasource level

 //Query                   query = new Query();

                //QueryBuildDataSource    queryBuildDataSource;

                //SysTableLookup          sysTableLookup;

                //sysTableLookup = SysTableLookup::newParameters(tableNum(EcoResProductCategory), _formControl);

                //queryBuildDataSource = query.addDataSource(tableNum(EcoResProductCategory));

                //sysTableLookup.addLookupField(fieldNum(EcoResProductCategory, Category));

                //sysTableLookup.addLookupField(fieldNum(EcoResProductCategory, CategoryHierarchy));

                //sysTableLookup.parmQuery(query);

                //sysTableLookup.performFormLookup();






Query                   query = new Query();

                var selectField = [tableNum(EcoResProductCategory), fieldNum(EcoResProductCategory, Category)];

                QueryBuildDataSource    queryBuildDataSource;

                SysTableLookup          sysTableLookup;

                sysTableLookup = SysTableLookup::newParameters(tableNum(EcoResProductCategory), _formControl);

                queryBuildDataSource = query.addDataSource(tableNum(EcoResProductCategory));

                queryBuildDataSource.addSelectionField(fieldNum(EcoResProductCategory, Category));

                var lookup =  MCOSysMultiselectionLookup::lookup(Query, selectField, MCOSAtoPO_Category,MCOSAtoPO_Category,

                                                                        MCOSAtoPO_Category,MCOSAtoPO_Category.valueStr());

                MCOSAtoPO.Category = lookup.getSelectedStringsJoined();








For 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();

    }


}