Blog

Name is Anant Dubey and the intent to create this blog is to discuss the problems and issues that developer face in the dynamics AX development and to share the new things that come up with the new version of AX.

Sunday, April 25, 2021

Multi-Select Lookup on Form Control in D365FO

 

/// <summary>

/// TSTLookupMultiSelectGridForDatasource to show lookup for a datasource field

/// </summary>

class TSTLookupMultiSelectGridForDatasource extends SysLookupMultiSelectGrid

{

    FormRun             callerFormRun;

    str                 callerFormdatasourceName;

    FieldId             populateFieldId;

   

 

    /// <summary>

    /// Get set Caller Form run

    /// </summary>

    /// <param name = "_callerFormRun">Caller form run</param>

    /// <returns>Caller form run</returns>

    FormRun parmCallerFormRun(FormRun _callerFormRun = callerFormRun)

    {

        callerFormRun = _callerFormRun;

 

        return callerFormRun;

    }

 

    /// <summary>

    /// Get set Caller datasource for lookup

    /// </summary>

    /// <param name = "_callerFormdatasourceName">Caller datasource for lookup</param>

    /// <returns>Caller datasource for lookup</returns>

    str parmCallerFormdatasourceName(str _callerFormdatasourceName = callerFormdatasourceName)

    {

        callerFormdatasourceName = _callerFormdatasourceName;

 

        return callerFormdatasourceName;

    }

 

    /// <summary>

    /// Get set field number for lookup

    /// </summary>

    /// <param name = "_populateFieldId">field number for lookup</param>

    /// <returns>field number for lookup</returns>

    FieldId parmPopulateFieldId(FieldId _populateFieldId = populateFieldId)

    {

        populateFieldId = _populateFieldId;

 

        return populateFieldId;

    }

 

    /// <summary>

    /// Set slected field to datasource field

    /// </summary>

    public void setSelected()

    {

        callingControlId.text(SysOperationHelper::convertMultiSelectedValueString(selectedId));

        str callingControlStrTxtVal = SysOperationHelper::convertMultiSelectedValueString(selectedStr);

        callingControlStr.text(callingControlStrTxtVal);

        if (callerFormRun && callerFormdatasourceName && populateFieldId)

        {

            FormDataSource  locDatasource = callerFormRun.dataSource(callerFormdatasourceName);

 

            if (locDatasource)

            {

                Common  recordTOPopulate = locDatasource.cursor();

 

                recordTOPopulate.(populateFieldId) = callingControlStrTxtVal;

            }

 

        }

    }

 

    /// <summary>

    /// Lookup multi select

    /// </summary>

    /// <param name = "_query">Query for lookup</param>

    /// <param name = "_callingCtrl">Calling control</param>

    /// <param name = "_ctrlIds">Control ids</param>

    /// <param name = "_ctrlStrs">Control strings</param>

    /// <param name = "_selectField">Selection fields</param>

    /// <param name = "_queryRun">Query run</param>

    /// <param name = "_formRun">Caller fi=ormrun</param>

    /// <param name = "_formDatasource">Caller data source</param>

    /// <param name = "_fieldId">Field id for populating lookup</param>

    public static void lookup(Query _query, FormStringControl _callingCtrl, FormStringControl _ctrlIds, FormStringControl _ctrlStrs, container _selectField, queryRun _queryRun = null,

        FormRun _formRun = null, str _formDatasource = '', FieldId _fieldId = 0)

    {

        TSTLookupMultiSelectGridForDatasource    lookupMS = new TSTLookupMultiSelectGridForDatasource();

 

        lookupMS.parmCallingControl(_callingCtrl);

        lookupMS.parmCallingControlId(_ctrlIds);

        lookupMS.parmCallingControlStr(_ctrlStrs);

        lookupMS.parmQuery(_query);

        lookupMS.parmQueryRun(_queryRun);

        lookupMS.parmSelectField(_selectField);

        lookupMS.parmCallerFormRun(_formRun);

        lookupMS.parmCallerFormdatasourceName(_formDatasource);

        lookupMS.parmPopulateFieldId(_fieldId);

        lookupMS.run();

    }

 

}


 

class TSTSampleFormEventHandler

{

    /// <summary>

    /// Lookup event handler of eentHanlder

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    [FormControlEventHandler(formControlStr(RetailTenderTypeTable, RetailTenderTypeTable_AllowedRefundTender), FormControlEventType::Lookup),

        SuppressBPWarning('BPParameterNotUsed', 'Parameter not required')]

    public static void MCSAllowedRefundTender_OnLookup(FormControl sender, FormControlEventArgs e)

    {

        Query                       query;

        FormRun                     formRunTT = sender.formRun();

        QueryBuildDataSource        tenderTypeQBDS;

        QueryBuildFieldList         enderTypeFieldList;

        FormStringControl           stringControl = sender as FormStringControl;

 

        query                       = new Query();

        tenderTypeQBDS = query.addDataSource(tableNum(RetailTenderTypeTable));

       

        enderTypeFieldList = tenderTypeQBDS.fields();

       

        enderTypeFieldList.dynamic(QueryFieldListDynamic::No);

        enderTypeFieldList.clearFieldList();

        enderTypeFieldList.addField(fieldNum(RetailTenderTypeTable, tenderTypeId));

        enderTypeFieldList.addField(fieldNum(RetailTenderTypeTable, name));

        enderTypeFieldList.addField(fieldNum(RetailTenderTypeTable, defaultFunction));

 

        TSTLookupMultiSelectGridForDatasource::lookup(query, stringControl, stringControl, stringControl, conNull(), null, formRunTT, formDataSourceStr(RetailTenderTypeTable, RetailTenderTypeTable), fieldNum(RetailTenderTypeTable, AllowedRefundTender));

    }

 

}

3 comments:

  1. Why not to use already existing SysLookupMultiSelectCtrl?

    ReplyDelete
  2. This isMultiselect Grid which can host multiple lookup fields like a HCMWorker lookup form

    ReplyDelete
  3. It is not selecting the selected field in the drop down

    ReplyDelete