Skip to main content

Notifications

Announcements

No record found.

Supply chain | Supply Chain Management, Commerce
Suggested answer

SysLookupMultiSelectGrid - Set container with values

Posted on by 84

Hello,

I am populating a SysLookupMultiSelectGrid using the following code

    /// 
    /// Multi select lookup method for country region Ids
    /// 
    /// 
    /// An object of _control class
    /// 
    private void lookupCountryRegionIds(FormStringControl _control)
    {
        container				conCountryRegionIds;

        gridCountries = new SysLookupMultiSelectGrid();
        gridCountries.parmCallingControl(_control);
        gridCountries.parmCallingControlId(_control);
        gridCountries.parmCallingControlStr(_control);
        gridCountries.parmQuery(this.CountryRegionIdsQuery());
        gridCountries.parmSelectField(conCountryRegionIds);
        gridCountries.setSelected();
        gridCountries.run();
    }
    
    private Query CountryRegionIdsQuery()
    {
        Query					query;
        QueryBuildDataSource	qbds;
        
        query = new Query();
        qbds = query.addDataSource(tableNum(LogisticsAddressCountryRegion));
        qbds.addSelectionField(fieldNum(LogisticsAddressCountryRegion, CountryRegionId));
        qbds.addSortField(fieldNum(LogisticsAddressCountryRegion, CountryRegionId));
        qbds.addGroupByField(fieldNum(LogisticsAddressCountryRegion, CountryRegionId));

        return query;
    }

I acheive the grid below:

pastedimage1669825986377v1.png

As you can see from that screenshot, whenever a country is selected, it is not set when the lookup method is executed. I am unsure how to set the conCountryRegionIds by the FormStringControl before I assign it to the parmSelectField().

Any ideas?

Thanks!

  • GirishS Profile Picture
    GirishS 27,832 Super User 2024 Season 1 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    Try the below code for postRun method. Try to get the dialog fields using bindInfo method and call then call override method of control.

    //Overriding postRun and commenting out super() to prevent
        //'RegisterOverrideMethod was called twice for the same object for method 'lookup'. You can only override a method once per instance.'
        public void postRun()
        {
            //super();
    
            dialog.dialogForm().formRun().controlMethodOverload(false);
            dialogFldCountryRegionId = this.bindInfo().getDialogField(
                contract,methodStr(AMFSalesInvoiceReportContract, parmCountryRegionId));
            dialogFldCountryRegionId.registerOverrideMethod(methodStr(FormStringControl, modified), methodStr(AMFSalesInvoiceReportUIBuilder, countryRegionModified), this);
    
            this.lookupInventSiteId();
            this.lookupInvoiceAccounts();
            this.lookupCountryRegionIds();
            this.lookupStateIds();
        }

    Thanks,

    Girish S.

  • Alex C Profile Picture
    Alex C 84 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    Hello Girish,

    Thanks for the reply. I had indeed performed the refreshQuery() however it doesn't seem to trigger the modified() method when I select the values. The only way I can get it to trigger is when I manually enter values in the combobox. Am I using the correct method for a "modified" trigger?

    Thanks

  • Suggested answer
    GirishS Profile Picture
    GirishS 27,832 Super User 2024 Season 1 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    SysLookupMultiSelectCtrl class will be called only one time and that to during control initialization. So, if you want, you can make use of the refreshQuery method on the SysLookupMultiSelectCtrl.

    Refer to the below thread.

    https://community.dynamics.com/365/financeandoperations/f/dynamics-365-for-finance-and-operations-forum/469509/a-control-with-the-name-already-exists-on-the-form-when-i-build-lookup-with-syslookupmultiselectctrl

    Thanks,

    Girish S.

  • Alex C Profile Picture
    Alex C 84 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    Hello Girish,

    I've managed to add my dialog fields with the SysLookupMultiSelectCtrl. However I am unable to filter using the modified() override. See full code below:

    class AMFSalesInvoiceReportUIBuilder extends SysOperationAutomaticUIBuilder
    {
        AMFSalesInvoiceReportContract	contract;
    
        DialogField		dialogFldInventSite;
        DialogField		dialogFldFromDate;
        DialogField		dialogFldToDate;
        DialogField		dialogFldInvoiceAccount;
        DialogField     dialogFldCountryRegionId;
        DialogField     dialogFldStateId;
    
        SysLookupMultiSelectGrid     gridCountries;
        SysLookupMultiSelectCtrl     ctrlCountries;
        SysLookupMultiSelectCtrl     ctrlSites;
        SysLookupMultiSelectCtrl     ctrlInvoiceAccs;
        SysLookupMultiSelectCtrl     ctrlStateIds;
    
        public void build()
        {
            contract = this.dataContractObject() as AMFSalesInvoiceReportContract;
    
            dialogFldFromDate       = this.addDialogField(methodStr(AMFSalesInvoiceReportContract, parmInvoiceFromDate), contract);
            dialogFldToDate         = this.addDialogField(methodStr(AMFSalesInvoiceReportContract, parmInvoiceToDate), contract);
            dialogFldInventSite     = this.addDialogField(methodStr(AMFSalesInvoiceReportContract, parmInventSiteId), contract);
            dialogFldInvoiceAccount = this.addDialogField(methodStr(AMFSalesInvoiceReportContract, parmInvoiceAccount), contract);
            dialogFldCountryRegionId = this.addDialogField(methodStr(AMFSalesInvoiceReportContract, parmCountryRegionId), contract);
            dialogFldStateId        = this.addDialogField(methodStr(AMFSalesInvoiceReportContract, parmStateId), contract);
    
            dialogFldInventSite.lookupButton(2);
            dialogFldInvoiceAccount.lookupButton(2);
            dialogFldCountryRegionId.lookupButton(2);
            dialogFldStateId.lookupButton(2);
        }
    
        //Overriding postRun and commenting out super() to prevent
        //'RegisterOverrideMethod was called twice for the same object for method 'lookup'. You can only override a method once per instance.'
        public void postRun()
        {
            //super();
    
            dialog.dialogForm().formRun().controlMethodOverload(false);
    
            dialogFldCountryRegionId.registerOverrideMethod(methodStr(FormStringControl, modified), methodStr(AMFSalesInvoiceReportUIBuilder, countryRegionModified), this);
    
            this.lookupInventSiteId();
            this.lookupInvoiceAccounts();
            this.lookupCountryRegionIds();
            this.lookupStateIds();
        }
    
        public void countryRegionModified(FormStringControl _control)
        {
            this.lookupStateIds();
        }
    
        public void lookupStateIds()
        {
            TableId multiSelectTableNum = tableNum(LogisticsAddressState);
            Query					query;
            QueryBuildDataSource	qbds;
            QueryBuildRange         qbr;
            
            container				selectedFields;
            container				conCountryRegionIds;
            int                     counter;
            
            query = new Query();
            qbds = query.addDataSource(tableNum(LogisticsAddressState));
            qbds.addSelectionField(fieldNum(LogisticsAddressState, Name));
            qbds.addSelectionField(fieldNum(LogisticsAddressState, CountryRegionId));
            qbds.addSortField(fieldNum(LogisticsAddressState, Name));
            qbds.addGroupByField(fieldNum(LogisticsAddressState, Name));
            selectedFields = [multiSelectTableNum, fieldNum(LogisticsAddressState, Name)];
    
            if (ctrlCountries)
            {
                conCountryRegionIds = ctrlCountries.getSelectedFieldValues();
                
                //Loop over the container values and apply range to query
                for(counter = 1; counter <= conLen(conCountryRegionIds); counter  )
                {
                    if (conPeek(conCountryRegionIds, counter) != '')
                    {
                        qbds.addRange(fieldNum(LogisticsAddressState, CountryRegionId)).value(queryValue(LogisticsAddressCountryRegion::findRecId(conPeek(conCountryRegionIds, counter)).CountryRegionId));
                    }               
                }
            }
    
            //If the container conCountryRegionIds, there will be a range to be applied
            if (ctrlStateIds)
            {
                ctrlStateIds = SysLookupMultiSelectCtrl::constructWithQuery(this.dialog().dialogForm().formRun(),
                                                                         this.dialogFldStateId.control(), query,
                                                                         false, selectedFields);
            }
            else
            {
                ctrlStateIds.refreshQuery(query);
            }
        }
    
        public void lookupInvoiceAccounts()
        {
            TableId multiSelectTableNum = tableNum(CustTable);
            Query					query;
            QueryBuildDataSource	qbds;
            container				selectedFields;
    
            query = new Query();
            qbds = query.addDataSource(multiSelectTableNum);
            qbds.addSelectionField(fieldNum(CustTable, InvoiceAccount));
            selectedFields = [multiSelectTableNum, fieldNum(CustTable, InvoiceAccount)];
    
            ctrlInvoiceAccs = SysLookupMultiSelectCtrl::constructWithQuery(this.dialog().dialogForm().formRun(),
                                                                         this.dialogFldInvoiceAccount.control(), query,
                                                                         false, selectedFields);
        }
    
        /// 
        /// Multi select lookup method for country region Ids
        /// 
        /// 
        /// An object of _control class
        /// 
        public void lookupCountryRegionIds()
        {
            TableId multiSelectTableNum = tableNum(LogisticsAddressCountryRegion);
            Query					query;
            QueryBuildDataSource	qbds;
            container				selectedFields;
            
            query = new Query();
            qbds = query.addDataSource(multiSelectTableNum);
            qbds.addSelectionField(fieldNum(LogisticsAddressCountryRegion, CountryRegionId));
            qbds.addSortField(fieldNum(LogisticsAddressCountryRegion, CountryRegionId));
            qbds.addGroupByField(fieldNum(LogisticsAddressCountryRegion, CountryRegionId));
            selectedFields = [multiSelectTableNum, fieldNum(LogisticsAddressCountryRegion, CountryRegionId)];
    
            ctrlCountries = SysLookupMultiSelectCtrl::constructWithQuery(this.dialog().dialogForm().formRun(),
                                                                         this.dialogFldCountryRegionId.control(), query,
                                                                         false, selectedFields);
        }
    
        //to populate lookup
        public void lookupInventSiteId()
        {
            TableId multiSelectTableNum = tableNum(InventSite);
            Query query = new Query();
            QueryBuildDataSource qbds = query.addDataSource(multiSelectTableNum);
            container selectedFields;
            
            qbds.addSelectionField(fieldNum(InventSite, SiteId));
            qbds.addSelectionField(fieldNum(InventSite, Name));
            selectedFields = [multiSelectTableNum, fieldNum(InventSite, SiteId)];
    
            ctrlSites = SysLookupMultiSelectCtrl::constructWithQuery(this.dialog().formRun(),
                                                                    this.dialogFldInventSite.control(), query, false,
                                                                    selectedFields);
        }
    }

    When selecting the values in my country region IDs, it doesn't trigger the modified() method. Any ideas?

  • GirishS Profile Picture
    GirishS 27,832 Super User 2024 Season 1 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    In most of the standard classes you can see SysLookupMultiSelectCtrl class is used for multi select lookup.

    SysLookupMultiSelectGrid is less used.

    No major difference but you can avoid this type of issue when using SysLookupMultiSelectCtrl.

    Thanks

    Girish S.

  • Alex C Profile Picture
    Alex C 84 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    Hi Girish,

    I have not solved my issue with the SysLookupMultiSelectGrid. I may look into the SysLookupMultiSelectCtrl next week, not sure why it would be better to use that one though?

    Thanks

  • GirishS Profile Picture
    GirishS 27,832 Super User 2024 Season 1 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    Have you solved your issue?

    Do you need any support.

    Thanks,

    Girish S.

  • GirishS Profile Picture
    GirishS 27,832 Super User 2024 Season 1 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    Why can't you use SysLookupMultiSelectCtrl?

    It will work similar to that.

    Can you tell me the reason why you have chosen SysLookupMultiSelectGrid ?

    It's better to use SysLookupMultiSelectCtrl instead of SysLookupMultiSelectGrid.

    Refer to the below link for implementing SysLookupMultiSelectCtrl

    https://axhowto.wordpress.com/2018/02/09/how-to-create-a-multi-select-lookup-and-save-values-in-table/

    Thanks,

    Girish S.

  • Alex C Profile Picture
    Alex C 84 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    Hello Girish,

    I removed the group by and unfortunately the results remain the same.

    Thanks

  • Suggested answer
    GirishS Profile Picture
    GirishS 27,832 Super User 2024 Season 1 on at
    RE: SysLookupMultiSelectGrid - Set container with values

    Hi Alex,

    I think the issue is because of group by you have added in the query. Just remove the group by and check whether its setting the selected value to lookup control.

    I think group by is not needed for country region table.

    Thanks,

    Girish S.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans