How to Override a Form Data Source Field Lookup Method in D365 F&O

How to Override a Form Datasource field Lookup method in D365 F&O? 

Scenario: The lookup field should show only active material IDs from the material master table. Inactive or obsolete material IDs should be excluded from the lookup. This ensures users can only select valid, active materials, maintaining data integrity and correct material selection. 

High Resolution steps 

    • Set the Material ID as Active in the Material Master Table. 
    • Implement Logic in the Lookup of the Form DataSource Field Level. 
    • Test the Lookup. 

    Detailed Resolution steps 

      Step 1: Set the Material ID as Active in the Material Master Table. 

      To set the Material ID as active, update the IsActive field to Yes for the materials you want available in the lookup. Ensure inactive materials have the IsActive field set to No

      Step 2: Implement Logic in the Lookup of the Form DataSource Field Level. 

      Go-to ->Form->Datasource->Field ->Right click -> override -> lookup.  

      Write the logic for fetching active material Id inside the Lookup method. 

      Code Snippet: 

      [DataField] 
      
      class MaterialId  
      
      { 
      
          ///  
      
          /// lookup for the Active materialId. 
      
          ///  
      
          ///  
      
          ///  
      
          public void lookup(FormControl _formControl, str _filterStr) 
      
          { 
      
              SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tableNum(ANKMaterialMasterTable), _formControl); 
      
              //Create a new query 
      
              Query                   query = new Query(); 
      
              QueryBuildDataSource    queryBuildDataSource; 
      
              QueryBuildRange         queryBuildRange; 
      
       
      
              queryBuildDataSource = query.addDataSource(tableNum(ANKMaterialMasterTable)); 
      
              queryBuildRange = queryBuildDataSource.addRange(fieldNum(ANKMaterialMasterTable,Active)); 
      
              queryBuildRange.value(queryValue(NoYes::Yes)); 
      
       
      
              sysTableLookup.addLookupfield(fieldNum(ANKMaterialMasterTable, MaterialId)); 
      
              sysTableLookup.addLookupfield(fieldNum(ANKMaterialMasterTable, MaterialName)); 
      
              sysTableLookup.addLookupfield(fieldNum(ANKMaterialMasterTable, Active)); 
      
       
      
              sysTableLookup.parmQuery(query); 
      
              sysTableLookup.performFormLookup(); 
      
          } 
      
      }       

      Step 3:Test the Lookup. 

      Test the lookup by navigating to the UI and checking the Material ID dropdown. If only active Material IDs are being populated, then the lookup functionality is working correctly. 

      OUTPUT 

      Below is the results, where the lookup now only displays active Material IDs, as it filters the results to include only records where the Is Active field is set to Yes. This ensures that only valid, active materials are shown in the lookup.