Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

Overwrite lookup method on form datasource field

(0) ShareShare
ReportReport
Posted on by 284

Hello everyone,

There is a form called InventNonConformanceTableCreate that I have added a new field to

The datasource of that form is InventNonConformanceTable

I created a new EDT and new field called TCI_InventProblemSubTypeId

The information for this field is in a table called TCI_InventProblemSubType which has the following fields:

Description
ProblemTypeId (related to table InventProblemType)

ProblemSubTypeId

Here is what it looks like on the form

pastedimage1676577994548v1.png

What I'm trying to accomplish is overwriting the lookup for that Problem subtype so that it only shows the subtypes that are related to the chosen Problem Type.

I've done this before on a form we created which made it easy.  I could right-click on the methods of the field in the datasource and overwrite the lookup.  Now I'm not sure how to do this correctly.

I've tried a few different ways but so far nothing has worked.

This is my code from the other form that we created that works great

[DataField]
class InventLocationId 
{
    /// 
    ///
    /// 
    /// 
    /// 
    public void lookup(FormControl _formControl, str _filterStr)
    {
        //Specify the name of the table the lookup should show data from.
        SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tableNum(InventLocation), _formControl);

        //Create a new query
        Query                   query = new Query();
        QueryBuildDataSource    queryBuildDataSource;
        QueryBuildRange         queryBuildRange;

        //Specify the name of the table the lookup should show data from.
        queryBuildDataSource = query.addDataSource(tableNum(InventLocation));
        queryBuildRange = queryBuildDataSource.addRange(fieldNum(InventLocation,InventSiteId));
        queryBuildRange.value(queryValue(TCI_ItemTable.InventorySite));

        //Specify which fields should be shown  in the lookup form.
        //  field returned is the first field referenced
        sysTableLookup.addLookupfield(fieldNum(InventLocation, InventLocationId));

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

}

Now I can easily adjust this code to look at the right tables and return the right fields, but where do I put this to get it to run?

  • Suggested answer
    Andrew Huisman Profile Picture
    Andrew Huisman 284 on at
    RE: Overwrite lookup method on form datasource field

    Hey everyone,

    I had another example on a form with multiple tabs and I needed to do a lookup of a field.  The problem is, that field was in many tabs so I could have wrote code for every control I found, but I didn't find that very efficient.  In a perfect world, I would put the lookup on the datasource field and that way it will happen no matter where it is.

    So first I found this article which started me in the right direction

    AX 7. How to override form data source field methods without overlaying. – ievgen's ax blog (wordpress.com)

    Next I needed to make a lookup method, so here is my code

    First I created the Override Class

    public class InventNonConformanceTableFormExtensionOverrides
    {
        protected void new()
        {
        }
    
        public static InventNonConformanceTableFormExtensionOverrides construct()
        {
            return new InventNonConformanceTableFormExtensionOverrides();
        }
    
        public void TCI_InventProblemSubTypeId_OnLookup(FormStringControl _callingControl)
        {
            //Specify the name of the table the lookup should show data from.
            SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tableNum(TCI_InventProblemSubType), _callingControl);
    
            FormRun         formRun = _callingControl.formRun();
            FormDataSource  dataSource_ds = _callingControl.formRun().dataSource();
            InventNonConformanceTable   inventNonConformanceTable = dataSource_ds.cursor();
    
            //Create a new query
            Query                   query = new Query();
            QueryBuildDataSource    queryBuildDataSource;
            QueryBuildRange         queryBuildRange;
    
            //Specify the name of the table the lookup should show data from.
            queryBuildDataSource = query.addDataSource(tableNum(TCI_InventProblemSubType));
            queryBuildRange = queryBuildDataSource.addRange(fieldNum(TCI_InventProblemSubType,ProblemTypeId));
            queryBuildRange.value(queryValue(inventNonConformanceTable.InventTestProblemTypeId));
    
            //Specify which fields should be shown  in the lookup form.
            //field returned is the first field referenced
            sysTableLookup.addLookupfield(fieldNum(TCI_InventProblemSubType, ProblemSubTypeId));
            sysTableLookup.addLookupfield(fieldNum(TCI_InventProblemSubType, Description));
    
            sysTableLookup.parmQuery(query);
            sysTableLookup.performFormLookup();
        }
    
    }

    Next created the Form Handlers Class

    public class InventNonConformanceTable_Form_Handlers
    {
        /// 
        ///
        /// 
        /// 
        /// 
        [FormDataSourceEventHandler(formDataSourceStr(InventNonConformanceTable, InventNonConformanceTable), FormDataSourceEventType::Initialized)]
        public static void InventNonConformanceTable_OnInitialized(FormDataSource sender, FormDataSourceEventArgs e)
        {
            var overrides = InventNonConformanceTableFormExtensionOverrides::construct();
    
            sender.object(fieldNum(InventNonConformanceTable, TCI_InventProblemSubTypeId)).registerOverrideMethod(methodStr(FormDataObject, lookup),
                methodStr(InventNonConformanceTableFormExtensionOverrides, TCI_InventProblemSubTypeId_OnLookup), overrides);
        }
    }

    Now the lookup runs for every control attached to the field which is exactly what I wanted.

  • Andrew Huisman Profile Picture
    Andrew Huisman 284 on at
    RE: Overwrite lookup method on form datasource field

    Thanks for all your help!  It is now working as expected!

  • Verified answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Overwrite lookup method on form datasource field

    Setting the Auto declaration to yes will work if its COC.

    To get form control values using event handler then refer to the below code.

    FormRun         formRun = sender.formRun();
    FormDataSource  dataSoruce_ds = sender.formRun().dataSource();
    //to get the buffer use below code.
    TableName       tableBuffer = dataSoruce_ds.cursor();
    //to get control value refer to beloe code
    FormControl     controlName = formRun.design().controlName("Name of the control");
    qbr.value(queryvalue(controlName.valueStr());

    Thanks,

    Girish S.

  • Andrew Huisman Profile Picture
    Andrew Huisman 284 on at
    RE: Overwrite lookup method on form datasource field

    Hi Girish,

    I was able to do what you said, and create a new group to replace the other.  Then I put in the following code for my control

    pastedimage1676994790562v1.png

    As you can see in the image, I hardcoded a queryvalue, just to see if it worked and it worked perfect.

    So now, I want to take the value of the control called InventNonConformanceTable_InventTestProblemTypeId and use that value as my queryvalue.

    I set the autodeclare on the control to Yes, and then put in InventNonConformanceTable_InventTestProblemTypeId.valueStr() in my queryvalue, but it tells me that InventNonConformanceTable_InventTestProblemTypeId has not been declared.  I also tried InventNonConformanceTable_InventTestProblemTypeId.value() but the same error is coming up.

    What should I be doing differently to get that value?

  • Verified answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Overwrite lookup method on form datasource field

    First check whether the lookup code for that control is working.

    If its working we can check about the position of the control.

    For the control to be on the specific group there is one work around.

    You can hide the existing group and create new group with all the controls including your custom controls.

    That's the only workaround you have.

    Thanks,

    Girish S.

  • Andrew Huisman Profile Picture
    Andrew Huisman 284 on at
    RE: Overwrite lookup method on form datasource field

    Girish,

    You are right.  How I got that control added to the form was by creating a table extension of InventNonConformanceTable and adding the field to the field group

    pastedimage1676652236594v1.png

    That is what automatically made it show up on the form

    If I add the control to the form on it's own and then take that event handler, it works, but the control is completely off where it should be on the form

    2023_2D00_02_2D00_17_5F00_11_2D00_45_2D00_16.png

    And here is where they are on the form

    2023_2D00_02_2D00_17_5F00_11_2D00_47_2D00_38.png

    I really need it in the first spot for it to make sense, but I also need to lookup to work.  I haven't tried my lookup code yet.....do we get that working first and then worry about the position of the control, or should we get the control in the right spot, and then worry about the code?

  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Overwrite lookup method on form datasource field

    If its in same model, controls added to the extended form will be bold in color.

    But in your screenshot the control and group is not bold. I am not sure the control and the event handler code is in same model.

    Try removing the field groups and manually add the controls and check.

    Also rename the control name and then copy the lookup event handler.

    Thanks,

    Girish S.

  • Andrew Huisman Profile Picture
    Andrew Huisman 284 on at
    RE: Overwrite lookup method on form datasource field

    Hi Giresh,

    Everything is in the same model.

    I removed the event handler code from the class and also deleted the other class with the extension of the datasource field.

    I then did a build and got no errors

    I went into the form, found my control (which is in a field group), right-clicked on the OnLookup event for it and chose Copy event handler method

    pastedimage1676650905746v1.png

    I pasted that method into my class

    pastedimage1676650951052v2.png

    I do a build and get this error

    pastedimage1676650992733v3.png

  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Overwrite lookup method on form datasource field

    Does your extension form, and event handler is in same model or in different model.

    Please remove the code in the event handler class and then try building the project.

    After successful build, paste the copied event handler code.

    Thanks,

    Girish S.

  • Andrew Huisman Profile Picture
    Andrew Huisman 284 on at
    RE: Overwrite lookup method on form datasource field

    I haven't written anything because it won't compile

    Here is my code

    public class InventNonConformanceTableCreate_Form_Handlers
    {
        /// 
        ///
        /// 
        /// 
        /// 
        [FormControlEventHandler(formControlStr(InventNonConformanceTableCreate, CreateInformation_TCI_InventProblemSubTypeId), FormControlEventType::Lookup)]
        public static void CreateInformation_TCI_InventProblemSubTypeId_OnLookup(FormControl sender, FormControlEventArgs e)
        {
            info("got here");
        }
    
    }

    And here is the build error

    pastedimage1676644324208v1.png

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans