Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Custom lookup in form datasource field

(0) ShareShare
ReportReport
Posted on by 685

Hi everybody.

I'm trying to create a simple lookup in a field of a datasource in a form.

The tables and related forms are BankAccountTable.

1.- Added new field BullCust (extends EDT CustAccount) and field group to new extension BankAccountTable.MyExtension.

2.- Created new form extension BankAccountTable.MyExtension and added new group to form.

This works OK. But now I want a simpler lookup. In Ax2012, I would override BankAccountTable\Datasources\BankAccountTable\BullCust\Lookup method, with something like this:

public void lookup(FormControl _formControl, str _filterStr)
    {
        Query                   query = new Query();
        QueryBuildDataSource    queryBuildDataSource;
        SysTableLookup          sysTableLookup;

        sysTableLookup = SysTableLookup::newParameters(tableNum(CustTable), _formControl);
        queryBuildDataSource = query.addDataSource(tableNum(CustTable));

        sysTableLookup.addLookupField(fieldNum(CustTable, AccountNum));
        sysTableLookup.addLookupField(fieldNum(CustTable, Party));
        sysTableLookup.addLookupField(fieldNum(CustTable, RecId));

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

In Ax7 this doesn't work anymore. Tried this approach (based on https://ievgensaxblog.wordpress.com/2016/05/01/ax-7-how-to-override-form-data-source-field-methods-without-overlaying/):

3.- Created class that manages lookup:

public class BullFormBankAccountTable_Handler
{
    public static BullFormBankAccountTable_Handler construct()
    {
        return new BullFormBankAccountTable_Handler();
    }

    public void BankAccountTable_BullCust_OnLookup(FormDataObject _formDataObject, FormControl _formControl, str _filterStr)
    {

        Query                   query = new Query();
        QueryBuildDataSource    queryBuildDataSource;
        SysTableLookup          sysTableLookup;

        sysTableLookup = SysTableLookup::newParameters(tableNum(CustTable), _formControl);
        queryBuildDataSource = query.addDataSource(tableNum(CustTable));

        sysTableLookup.addLookupField(fieldNum(CustTable, AccountNum));
        sysTableLookup.addLookupField(fieldNum(CustTable, Party));
        sysTableLookup.addLookupField(fieldNum(CustTable, RecId));

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

}

4.- Created class extension that subscribes and links apropiated events:

[ExtensionOf(formStr(BankAccountTable))]
final public class BullFormBankAccountTable_Extension
{
    [FormDataSourceEventHandler(formDataSourceStr(BankAccountTable, BankAccountTable), FormDataSourceEventType::Initialized)]
    public static void BankAccountTable_OnInitialized(FormDataSource _sender, FormDataSourceEventArgs _e)
    {
        var overrider = BullFormBankAccountTable_Handler::construct();
  
        _sender.object(fieldNum(BankAccountTable, BullCust)).registerOverrideMethod(methodStr(FormDataObject, lookup),
            methodStr(BullFormBankAccountTable_Handler, BankAccountTable_BullCust_OnLookup), overrider);
    }

}

And no result. I've debugged and BullFormBankAccountTable_Extension.BankAccountTable_OnInitialized() is properly called. But resulting lookup is the standard one, not the mine one.
Any ideas or example about how should this be performed?

(PS: version Ax7 Platform Update 9)

*This post is locked for comments

  • Suggested answer
    Hossein.K Profile Picture
    Hossein.K 6,642 on at
    RE: Custom lookup in form datasource field

    Hi,

    please check this link;

    https://msdn.microsoft.com/en-us/library/hh185372.aspx

  • Suggested answer
    Mahesh Holla Profile Picture
    Mahesh Holla 30 on at
    RE: Custom lookup in form datasource field

    Hi Raul,

    I tweaked the code which you have used and the lookup is working fine for me.

    https://axvarsity.blogspot.com/2018/08/lookup-for-form-control-in-d365.html

  • RE: Custom lookup in form datasource field

    In my testings, the registerOverrideMethod on performFormLookup() worked... the first time. Second times and further, a fixed and non-closable form is shown; so I had to nevermind this way.

    Finally I used control events approach suggested by Martin, but this is far from ideal.

    I was wondering if Chain Of Commands (new from PU9) work over form methods, datasources, fields, and controls??

  • Mea_ Profile Picture
    Mea_ 60,278 on at
    RE: Custom lookup in form datasource field

    Hi Raúl Llorente Peña,

    Thanks for reading my blog :) There is a workaround, registerOverrideMethod works for performFormLookup() method and you can change a query there, however, you cannot do all the things that are available through lookup method. However, check this blog maybe it will help you http://romacode.com/blog/how-to-create-a-lookup-in-dynamics-ax-that-is-conditional-based-on-a-different-field

  • RE: Custom lookup in form datasource field

    Martin, there's a reasonable chance that this cannot be performed due to a known bug (reported since May'16 at least). Tested this approach with other methods and worked, but with lookup() method it fails.

  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    RE: Custom lookup in form datasource field

    Calm down, I just wanted you to consider the option. I obviously don't know your particular situation.

    If you aim for reusability, you may want to set a lookup form to the Form Help property of an EDT and then use the EDT for the field - and all other table fields that should use the same datasource.

    I'll try to debug your code tomorrow.

  • RE: Custom lookup in form datasource field

    Martin, what if an user decides to customize the form and add BullCust field into other place of the form? No custom lookup would appear.

    And what if the control is part of a group?  It's not very advisable to edit control's methods in this case...

    And what if the control appears three times in the form? Should I triplicate the code?

  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    RE: Custom lookup in form datasource field

    Why don't you simply add a form control and handle its OnLookup event?

  • AX 2012 r3 Profile Picture
    AX 2012 r3 2,420 on at
    RE: Ax7 - Custom lookup in form datasource field

    Hi,

    you may find any clue.

    http://www.microsoftdynamicsax365.com/?s=lookup

    Regards.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans