Skip to main content

Notifications

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!

  • 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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,060 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,858 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans