Tuesday, May 21, 2019

Multiselect lookup form control on D365 FO



//Event Handler code for Onlookup event

//Form Control with DS associate

FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;

        FormDataSource fds;
        FormControl fc = sender as FormControl;
        fds = fc.formRun().Datasource(tableStr(<DS Name>));

        FormRun fr = fc.formRun();

        FormStringControl   DummyrecIds = fr.design().controlName(formControlStr(<Form Name>, DummyRecIds)) as FormStringControl; //Create control on form and make it visible NO and Auto declaration yes. Its for dummy purpose
        FormStringControl   DummyStrIds = fr.design().controlName(formControlStr(<Form Name>, DummyStrIds)) as FormStringControl; //Create control on form and make it visible NO and Auto declaration yes.Its for dummy purpose

        TableName    tableBuffer= fds.cursor();

        Query lookupQuery = new Query();

        var selectField = [tableNum(<Table Name>), fieldNum(<Table Name>, <Field Name>)];

        QueryBuildDataSource qbds= lookupQuery.addDataSource(tableNum(<Table Name>));
        qbds.addSelectionField(fieldNum(<Table Name>, Name));

        San_SyslookupMultiSelectGrid  lookupLocation;
        lookupLocation = new San_SyslookupMultiSelectGrid();
     
        lookupLocation = San_SyslookupMultiSelectGrid::lookup(lookupQuery,
                                                                selectField,
                                                                sender,
                                                                DummyrecIds,
                                                                DummyStrIds,  <Table Name>.<FieldName>);

         
        <Table Name>.<FieldName> = lookupLocation.getSelectedStringsJoined();

        event.CancelSuperCall(); //Cancel default lookup call on string control

//Event handler code end


//Lookup class
class San_SyslookupMultiSelectGrid extends SysLookupMultiSelectGrid
{
    #SysOperation
    private const str CollectionValueSeparator = #CollectionValueSeparator;

    Public static San_SyslookupMultiSelectGrid lookup(Query _query, container _selectField, FormStringControl _callingCtrl, FormStringControl _recIdsControl, FormStringControl _strIdsControl, str _currentStrIds, queryRun _queryRun = null)
    {
        _strIdsControl.text(_currentStrIds);
        _recIdsControl.text('');

        var lookupMS = new San_SyslookupMultiSelectGrid();

        lookupMS.parmCallingControl(_callingCtrl);
        lookupMS.parmCallingControlId(_recIdsControl);
        lookupMS.parmCallingControlStr(_strIdsControl);
        lookupMS.parmQuery(_query);
        lookupMS.parmQueryRun(_queryRun);
        lookupMS.parmSelectField(_selectField);
        lookupMS.run();

        return lookupMS;
    }

      Public container getSelectedStrings()
    {
        return selectedStr;
    }


    Public SysLookupMultiSelectValues getSelectedStringsJoined()
    {
        return SysOperationHelper::convertMultiSelectedValueString(selectedStr);
    }

      Public static boolean stringListContainsItem(str _stringList, str _possibleItem)
    {
        container items = str2con(_stringList, CollectionValueSeparator);
        for (int i = 1 ; i <= conLen(items) ; i++)
        {
            str item = conPeek(items, i);
            if (item == _possibleItem)
            {
                return true;
            }
        }
        return false;
    }

}


//Multi select lookup with Form Control without DS
FormRun fr = sender.formRun() as FormRun;
        Query                   query = new Query();
        QueryBuildDataSource    qbd;
        TableId     multiSelectTableNum = tableNum(ForecastModel);
        container   selectedFields      = [multiSelectTableNum, fieldName2id(multiSelectTableNum, fieldStr(ForecastModel, ModelId))];
      
        qbd = query.addDataSource(tableNum(ForecastModel));
        qbd.addSelectionField(fieldNum(ForecastModel, ModelId));
        qbd.fields().dynamic(NoYes::No);
        qbd.fields().clearFieldList();
        qbd.fields().addField(fieldNum(ForecastModel, ModelId));
        qbd.fields().addField(fieldNum(ForecastModel, Txt));
      
        SysLookupMultiSelectCtrl::constructWithQuery(fr, sender, query, false, selectedFields);

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...