web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Active Discussion

Form control lookup extension via Chain of Command

(0) ShareShare
ReportReport
Posted on by 15

Hi All,

I've been struggling to create a Chain of Command-based form control lookup override for a while now, and I didn't see anything on it in the forums, so I thought I'd share.  First, just a reminder of the event-based approach we all know and hate (okay, maybe just I hate it .. I just don't want to use attributes on my code if I don't have to). 

An example of what I'll call the "old way":

class ExampleForm_EventHandler
{
    [FormEventHandler(formStr(FormImChanging), FormEventType::Initialized)]
    public static void FormImChanging_OnInitializing(xFormRun sender, FormEventArgs e)
    {
        ExampleForm_EventHandler overrideHandler = new ExampleForm_EventHandler();

        FormStringControl targetControl = sender.control(sender.controlId(formControlStr(FormImChanging, targetControl)));
        targetControl.registerOverrideMethod(methodStr(FormDataObject, lookup), methodStr(ExampleForm_EventHandler, lookupOverride), overrideHandler);
    }

    public void lookupOverride(FormStringControl _callingControl)
    {
        SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(SomePertinentTable), _callingControl);

        sysTableLookup.addLookupfield(fieldNum(SomePertinentTable, UsefulField1), true);
        sysTableLookup.addLookupfield(fieldNum(SomePertinentTable, UsefulField2));

        Query q = new Query();

        q.addDataSource(tableNum(SomePertinentTable)).addRange(fieldNum(SomePertinentTable, NoYesFieldIWantToConstrain)).value(queryValue(NoYes::No));

        // Run lookup
        sysTableLookup.parmQuery(q);
        sysTableLookup.performFormLookup();
    }
}

Now, the "new way" (Chain of Command) that I finally got working today (using the same bogus names from the above code sample):

[ExtensionOf(formstr(ExampleForm))]
final class ExampleForm_FormIan_Extension
{    
    void init()
    {
        next init();
    
        //Assumes TargetControl is auto-declared, but we can get to it even if it's not
        TargetControl.registerOverrideMethod(methodStr(FormDataObject, lookup), formMethodStr(ExampleForm, ianCustomLookup));
    }

    public void ianCustomLookup(FormStringControl _callingControl)
    {
        SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(SomePertinentTable), _callingControl);

        sysTableLookup.addLookupfield(fieldNum(SomePertinentTable, UsefulField1), true);
        sysTableLookup.addLookupfield(fieldNum(SomePertinentTable, UsefulField2));

        Query q = new Query();

        q.addDataSource(tableNum(SomePertinentTable)).addRange(fieldNum(SomePertinentTable, NoYesFieldIWantToConstrain)).value(queryValue(NoYes::No));

        // Run lookup
        sysTableLookup.parmQuery(q);
        sysTableLookup.performFormLookup();
    }
}

Hope this helps somebody!

  • Blue Wang Profile Picture
    Microsoft Employee on at

    Hi Ian,

    Thank you for sharing, but it is better to apply for a blog to share it.

    Thanks again.

  • ijensen Profile Picture
    15 on at

    Thank you Blue - I went ahead and did that, we'll see what they think!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 468 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 333 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans