Skip to main content

D365F&O: Create and Use Custom Lookups (Updated Guide)

D365F&O: Create and Use Custom Lookups

In this article, we will cover how to create a lookup form and link it with an EDT or String field in Dynamics 365 Finance & Operations using Extensions and Chain of Command.

Technical Prerequisites

  • Chain of Command (CoC): We will use extension classes to override form control behavior.
  • Form Patterns: We will use the "Lookup Basic" pattern to ensure the form renders correctly as a popup in the web browser.

Scenario

We need to override the system lookup to apply custom logic, filters, or use a completely custom Form design as a lookup for a specific field.


1. Standard String/EDT Field Lookup (via Chain of Command)

Use this when you want to filter a specific field using X++ query logic without creating a new custom lookup form.

Class: Form Data Source Field Extension

// Override lookup for CustGroup field
[ExtensionOf(formDataFieldStr(CustTable, CustTable, CustGroup))]
final class CustTableFormDataField_Extension
{
    public void lookup(FormControl _formControl, str _filterStr)
    {
        Query query = new Query();
        QueryBuildDataSource qbds;
        SysTableLookup sysTableLookup;

        // Lookup on CustGroup table (Correct)
        sysTableLookup = SysTableLookup::newParameters(tableNum(CustGroup), _formControl);

        // Fields to show
        sysTableLookup.addLookupfield(fieldNum(CustGroup, CustGroup));
        sysTableLookup.addLookupfield(fieldNum(CustGroup, Name));

        // Build query
        qbds = query.addDataSource(tableNum(CustGroup));

        // Optional filtering example:
        // qbds.addRange(fieldNum(CustGroup, CustGroup)).value("10*");

        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
    }
}

2. Reference Group Lookup (RecId-based fields)

This applies when the lookup control is a Reference Group and you want to override its lookup logic.

Class: Form Control Extension

// Example control name: PayrollEarningCode
[ExtensionOf(formControlStr(HcmPosition, PayrollEarningCode))]
final class HcmPosition_PayrollEarningCode_Extension
{
    public Common lookupReference()
    {
        Query query = new Query();
        QueryBuildDataSource qbds;
        SysReferenceTableLookup sysRefTableLookup;

        FormReferenceGroupControl referenceControl = this;

        sysRefTableLookup = SysReferenceTableLookup::newParameters(
            tableNum(PayrollEarningCode),
            referenceControl
        );

        sysRefTableLookup.addLookupfield(fieldNum(PayrollEarningCode, EarningCode));
        sysRefTableLookup.addLookupfield(fieldNum(PayrollEarningCode, QuantityUnit));

        qbds = query.addDataSource(tableNum(PayrollEarningCode));

        sysRefTableLookup.parmQuery(query);

        return sysRefTableLookup.performFormLookup();
    }
}

3. Using a Custom Form as a Lookup

Use this method when you have built a custom lookup form (e.g., CustLookupForm) for complex UI behavior.

Class: Form Control Extension

[ExtensionOf(formControlStr(SalesTable, CustAccount))]
final class SalesTable_CustAccount_Extension
{
    public void lookup(FormControl _formControl, str _filterStr)
    {
        Args args = new Args();
        FormRun custLookup;

        args.name(formStr(CustLookupForm)); // Custom lookup form name
        args.caller(_formControl);

        custLookup = classFactory.formRunClass(args);
        custLookup.init();
        custLookup.run(); // Recommended for stability

        _formControl.performFormLookup(custLookup);
    }
}

How to Design a Lookup Form in D365

A lookup form must follow specific design patterns to render correctly in the browser.

  1. Create the Form: Create a new Form named CustLookupForm.
  2. Apply Form Pattern:
    Right-click the Design node → Apply PatternLookup Basic.
  3. Data Sources:
    • Add CustTable
    • Add DirPartyTable and join it to CustTable
    • Set AllowEdit, AllowCreate, AllowDelete to No
  4. Design Properties:
    Set Style = Lookup.
  5. Grid Layout:
    The Lookup Basic pattern requires:
    Design → Group → Grid
  6. Form Logic (init):
  7. public void init()
    {
        super();
        element.selectMode(CustTable_AccountNum); // Returns the row's value to caller when clicked
    }
    
  8. Form Logic (run): Handle filtering
  9. public void run()
    {
        FormStringControl callerControl;
        boolean filterLookup;
    
        callerControl = SysTableLookup::getCallerStringControl(element.args());
    
        // Filter before run()
        filterLookup = SysTableLookup::filterLookupPreRun(
            callerControl,
            CustTable_AccountNum,
            CustTable_ds
        );
    
        super();
    
        // Apply filter after form loads
        SysTableLookup::filterLookupPostRun(
            filterLookup,
            callerControl.text(),
            CustTable_AccountNum,
            CustTable_ds
        );
    }
    

Comments

Popular posts from this blog

Developing SSRS Reports Using Report Data Provider in D365F&O

Developing SSRS Reports Using Report Data Provider in D365F&O In this article, we will cover the Report Data Provider (RDP) class and the Report Contract class. We will also learn how to implement these classes for reporting needs in Dynamics 365 Finance & Operations and create a report using the RDP class via a step-by-step walkthrough. Report Data Provider (RDP) Framework A Report Data Provider (RDP) class is an X++ class used to access and process data for a report. An RDP class is an appropriate data source type when the following conditions are met: You cannot query directly for the data you want to render on a report. The data to be processed and displayed is complex and requires specific business logic. Report Data Provider Class This is an X++ class used to access and process data for an SSRS report. The RDP class processes business logic based on specified parameters and/or queries and return...

Fix SSRS Parameter Panel Layout Error

Fix SSRS Parameter Panel Layout Error [Fix] SSRS Error: "The parameter panel layout for this report contains more parameters than total cells available" Tags: #SSRS #Dynamics365 #SQLServer #ReportingServices #Troubleshooting If you have been working with SSRS (SQL Server Reporting Services) lately—especially within the Dynamics 365 environment—you might have encountered a frustrating error when trying to deploy a report after adding a new parameter. It usually happens when you add a Hidden Parameter or modify the existing list, and suddenly the deployment fails with this severity code: Error: The parameter panel layout for this report contains more parameters than total cells available. Source: Microsoft.ReportingServices.Library.ReportingService2005Impl.CreateReport The Scenario You have an existing report. You need to add a new par...

Developing SSRS Reports Using a Query in D365F&O

Developing SSRS Reports Using a Query in D365F&O In this tutorial, we will learn how to develop a report based on a predefined Query in the Application Object Tree (AOT) within Visual Studio. This method is often faster than using a Report Data Provider (RDP) class when complex business logic is not required. Scenario: We will create a report that prints a simple list of customer transactions directly from the CustTrans table without writing X++ business logic. Step 1: Create the Project and Query Unlike previous versions of Dynamics, all development for D365F&O occurs inside Visual Studio. Open Visual Studio as an administrator. Create a new Finance and Operations project. In the Solution Explorer , right-click the project node. Select Add > New Item . Select Query from the list and name it CustomerTransactionQuery . Click Add . Configuring the Data Source...