Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Multi select lookup only toggled once!

(1) ShareShare
ReportReport
Posted on by 190
I've 2 different lookup scenarios.. if the user chooses AccountNum field for VendTable, a lookup with all vendors show be shown for him to choose from..
and if he chooses VendGroup field he'll get a lookup with vendor groups.
 
The problem is:
When he choose AccountNum as field and use Vendors lookup and then add another record and choose VendGroup as a field.. the lookup will not show vendor groups but rather show all vendors instead and vice versa. 
 
 
 
So, it depends on the field he uses the lookup for first, if he chooses VendGroup then it'll show vendor groups always even if the field is AccountNum!
 
In other words, it doesn't toggle lookup method more than once!
 
Here's my code:
This is a global variable among the class
  SysLookupMultiSelectCtrl    msctrl;
 
and this is the code in the datasource field methods:
 
[DataField]
class Value 
{
    public void lookup(FormControl _formControl, str _filterStr)
    {   
        FormStringControl       frmStringCtrl;
        frmStringCtrl = element.design().controlName(formControlStr(SetupForm,Grid_Value));
    
        if(SetupTable.Field == 'VendGroup')
                {
                    Query                  q;
                    QueryBuildDatasource   qbds;

                    q           = new Query();
                    qbds        = q.addDataSource(tableNum(VendGroup));
                    qbds.addSelectionField(fieldNum(VendGroup, VendGroup));
                    qbds.addSelectionField(fieldNum(VendGroup, Name));
                    msctrl = SysLookupMultiSelectCtrl::constructWithQuery(_formControl.formRun(),frmStringCtrl,q);   
                }

        if(SetupTable.Field == 'AccountNum' && SetupTable.TableOption == TableOption::VendTable)
                {
                    Query                  q;
                    QueryBuildDatasource   qbds,qbds_DirPtyTable;
                   
                    q                = new Query();
                    qbds             = q.addDataSource(tableNum(VendTable));
                    qbds.addSelectionField(fieldNum(VendTable, AccountNum));

                    qbds_DirPtyTable = qbds.addDataSource(tableNum(DirPartyTable));
                    qbds.addSelectionField(fieldNum(DirPartyTable, Name));
                    qbds_DirPtyTable.relations(true);
                    msctrl = SysLookupMultiSelectCtrl::constructWithQuery(_formControl.formRun(),frmStringCtrl,q);
                }
    }


    public void modified()
    {
        SetupTable.Value = con2Str(msctrl.getSelectedFieldValues(),',');

        super();
    }
}
Categories:
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    Multi select lookup only toggled once!
    SysLookupMultiSelectCtrl is a normal X++ class, therefore we can see its implementation and debug it at runtime if needed.
     
    In init(), it registers a lookup() method override: ctrlNames_lookup() method of SysLookupMultiSelectCtrl class will be used instead of lookup() method of the form control. It'll then use SysLookupMultiSelectGrid form instead of your overridden lookup method.
     
    It uses the queryRun object as a parameter for creating the lookup, but you don't seem to provide a new one when switching to another scenario.
     
    In my opinion, you should remove your lookup() method completely, you should call SysLookupMultiSelectCtrl::constructWithQuery() just once (when initializing the form) and use msctrl.refreshQueryRun() to provide a new query when needed (e.g. in active() method of SetupTable data source).

    SysLookupMultiSelectCtrl.set() method can be used to provide selected values.
  • AbdullahAhmed_ Profile Picture
    AbdullahAhmed_ 190 on at
    Multi select lookup only toggled once!
    Thank you Martin.. I really learn a lot from your replies and hints.
     
    When I debug the lookup method It's only toggled the first time I use the lookup and when I use it on another field the debugger doesn't enter the lookup method at all.. it goes directly to modified method of the field. One more thing I've noticed that the values I've selected from the the first lookup are saved in other lookups.. that means when I use lookup for first time on vendor account and select (10,11,12) values from the lookup, when I add another record with VendGroup field and try to use the lookup it shows the vendors lookup again not the groups with the values (10,11,12) are selected -from the previous lookup-
     
    It's like the global variable msctrl of type "SysLookupMultiSelectCtrl" is like a container that has the values once and doesn't modify them.. I don't know how it prevents entering the lookup method!   
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    Multi select lookup only toggled once!
    What did you find when you debugged your lookup() method, before giving up and creating this thread? Does the expected logic gets called?
     
    By the way, you're using a wrong data source when adding DirPartyTable.Name.
     
    I also think that your code can be simplified (and improved) to something like this:
    public void lookup(FormControl _formControl, str _filterStr)
    {   
        Query q = new Query();
    
        if (setupTable.Field == fieldStr(VendGroup, VendGroup)
        {
            QueryBuildDatasource vendGroupQbds = q.addDataSource(tableNum(VendGroup));
            vendGroupQbds.addSelectionField(fieldNum(VendGroup, VendGroup));
            vendGroupQbds.addSelectionField(fieldNum(VendGroup, Name));
        }
        else if (setupTable.Field == fieldStr(VendTable, AccountNum)
            && setupTable.TableOption == TableOption::VendTable)
        {
            QueryBuildDataSource vendTableQbds = q.addDataSource(tableNum(VendTable));
            vendTableQbds.addSelectionField(fieldNum(VendTable, AccountNum));
    
            QueryBuildDataSource dirPartyQbds = vendTableQbds.addDataSource(tableNum(DirPartyTable));
            vendTableQbds.addSelectionField(fieldNum(DirPartyTable, Name));
            vendTableQbds.relations(true);
        }
    FormStringControl stringControl = _formControl; msctrl = SysLookupMultiSelectCtrl::constructWithQuery(element, stringControl, q); }

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans