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

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

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!

I have the same question (0)
  • ijensen Profile Picture
    15 on at
    RE: Form control lookup extension via Chain of Command

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

  • Blue Wang Profile Picture
    on at
    RE: Form control lookup extension via Chain of Command

    Hi Ian,

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

    Thanks again.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,239

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 767 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 672 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans