Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Lookup Filtering on x++
Finance forum
Answered

Lookup Filtering on x++

Posted on by 65

I am a beginner at d365 finance and operations. I have service and sub-service lookup fields in a form. This field's data sources have a one-to-many relationship with a foreign key. When I select a record in the service field, I want to see only sub-services that belong to the selected 'service' record in the 'sub-services lookup field. I need a lookup filtering for this with X but I do not know-how. I would be really appreciated it if someone can help me with this.

Here is my code but I really confused all the tables and fields name and I probably write the wrong names in the wrong places.

//My main form that includes lookup fields is inventSite

// Control names of lookup fields are InvenSite_ServiceRefRecId and InventSite_SubServiceRefRecId

//The name of the form that includes service and subservice records is Service

// the table that includes subservice records is SubService

Here is my Service form;

6663.Screen-Shot-2021_2D00_05_2D00_22-at-15.07.15.png

And here is my code;

[FormControlEventHandler(formControlStr(InventSite, InventSite_SubServiceRefRecId), FormControlEventType::Lookup)]
   public static void InventSite_SubServiceRefRecId_OnLookup(FormControl sender, FormControlEventArgs e)
   {
       SysReferenceTableLookup sysRefTableLookup;
       Query query = new Query();
       QueryBuildDataSource qbds;

       FormRun formRun;
       FormControl formCtrl;
       ServiceName serviceName;
       FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;

       sysRefTableLookup = SysReferenceTableLookup::newParameters(tableNum(Service), sender);
       qbds = query.addDataSource(tableNum(Service));
       formRun = sender.formRun();

        
       formCtrl = formRun.design().controlName(formControlStr(InventSite, InventSite_SubServiceRefRecId));
       serviceName = formCtrl.valueStr();

       qbds.firstOnly(true);
       qbds.addRange(fieldNum(Service, ServiceName)).value(queryValue(serviceName));

       sysRefTableLookup.parmQuery(query);
       sysRefTableLookup.addLookupfield(fieldNum(Service, ServiceName));
       sysRefTableLookup.addLookupfield(fieldNum(Service, ServiceDescription));
        
       sysRefTableLookup.performFormLookup();

       ce.CancelSuperCall();
   }

Thank you. Best Regards..

  • YCeren Profile Picture
    YCeren 65 on at
    RE: Lookup Filtering on x++

    Yes, you were right. I noticed that mistake and fixed it. it is working. Thank you very much for your help. Much appreciated.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 225,490 Super User on at
    RE: Lookup Filtering on x++

    Which line of code is throwing the error? I think the problem is that SysReferenceTableLookup::newParameters() still refers to Service, not SubService (if I'm right that you want to use SubService instead of Service, which you didn't confirm).

    By the way, please never hard-code any object names (such as "InventSite") if you can avoid it. I think that my approach (sender.dataSource().cursor()) is easier, but if you have to find a datasource by name, use formDataSourceStr(). These methods validate that the object exists and they also allow finding the usage through cross-references.

  • YCeren Profile Picture
    YCeren 65 on at
    RE: Lookup Filtering on x++

    Thank you very much for your answer and help I organized the code but It 'Failed to execute query because no root data source on the form matches the root datasource on the query.' I really need this is to work. I added SubService table to form datasource Why I keep getting this error?

    [FormControlEventHandler(formControlStr(InventSite, InventSite_SubServiceRefRecId), FormControlEventType::Lookup)]
        public static void InventSite_SubServiceRefRecId_OnLookup(FormControl sender, FormControlEventArgs e)
        {
            SysReferenceTableLookup tableLookup = SysReferenceTableLookup::newParameters(tableNum(Service), sender);
            Service_WM Service;
            FormRun formRun =  sender.formRun();
            FormControl formCtrl;
            Query query = new Query();
        
    
            QueryBuildDataSource qbds = query.addDataSource(tableNum(SubService));
            
            InventSite inventSite = sender.formRun().dataSource("InventSite").cursor();
            
    
    
            qbds.addRange(fieldNum(SubService, RecId)).value(queryValue(inventSite.ServiceRefRecId));
    
            tableLookup.parmQuery(query);
            tableLookup.addLookupField(fieldNum(SubService, SubServiceName));
            tableLookup.addLookupField(fieldNum(SubService, SubServiceDesc));
    
            tableLookup.performFormLookup();
    
            FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;
            ce.CancelSuperCall();
        }

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 225,490 Super User on at
    RE: Lookup Filtering on x++

    You said that you want filtered subservice, but you're selecting Service records. That sounds wrong to me.

    Also, I suspect that ServiceName field doesn't actually contain RecIds. Selecting just the first record doesn't look correct either.

    I think you want something like this:

    [FormControlEventHandler(formControlStr(InventSite, InventSite_SubServiceRefRecId), FormControlEventType::Lookup)]
    public static void InventSite_SubServiceRefRecId_OnLookup(FormControl sender, FormControlEventArgs e)
    {
    	SysReferenceTableLookup tableLookup = SysReferenceTableLookup::newParameters(tableNum(Service), sender);
    	
    	Query query = new Query();
    	QueryBuildDataSource subServiceDs = query.addDataSource(tableNum(SubService));
    	
    	InventSite inventSite = sender.dataSource().cursor();
    	qbds.addRange(fieldNum(SubService, ServiceRecId)).value(queryValue(inventSite.ServiceRecId));
    
    	tableLookup.parmQuery(query);
    	tableLookup.addLookupField(fieldNum(SubService, OneField));
    	tableLookup.addLookupField(fieldNum(SubService, AnotherField));
    
    	tableLookup.performFormLookup();
    
    	FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;
    	ce.CancelSuperCall();
    }

    You'll need to update field names to match your actual situation.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans