0

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 Service form

[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();
   }
Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
Granger
  • 21
  • 1
  • 10

1 Answers1

0

You need to overide lookup. Please find an example here in community blog. https://community.dynamics.com/365/financeandoperations/b/365foandaxtechnicalworld/posts/override-lookup-ax7-amp-d365fo

  • My code is similar to this actually, I added my code but I got confused with the tables and fields and I am afraid I put them in the wrong places. Can you please help me? – Granger May 22 '21 at 12:27
  • You wanted to filter subservice. So you need to use SubService table in below code instead of Service table. sysRefTableLookup = SysReferenceTableLookup::newParameters(tableNum(Service), sender); qbds = query.addDataSource(tableNum(Service)); qbds.addRange(fieldNum(Service, ServiceName)).value(queryValue(serviceName)); sysRefTableLookup.parmQuery(query); sysRefTableLookup.addLookupfield(fieldNum(Service, ServiceName)); sysRefTableLookup.addLookupfield(fieldNum(Service, ServiceDescription)); – Pradeep Muttikulangara Vasu May 26 '21 at 04:30
  • Then to get form control value of Service field you should be using InventSite_ServiceRefRecId and not "SubServiceRefRecId". formCtrl = formRun.design().controlName(formControlStr(InventSite, InventSite_SubServiceRefRecId)); – Pradeep Muttikulangara Vasu May 26 '21 at 04:35