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, ...
Answered

Overwrite lookup method on form datasource field

(0) ShareShare
ReportReport
Posted on by 313

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?

I have the same question (0)
  • Suggested answer
    Mohit Rampal Profile Picture
    12,563 Moderator on at
    RE: Overwrite lookup method on form datasource field

    Hi Andrew, You can create extension class for FormDatasource field methods.

    Check this thread and instead of modified method you can extend lookup method

    community.dynamics.com/.../extension-class-of-form-datasource-field-method

  • Suggested answer
    GirishS Profile Picture
    27,825 Moderator on at
    RE: Overwrite lookup method on form datasource field

    Hi Andrew,

    Since D365 doesn't allow you to add or change code directly in the standard objects, you need to make use of COC or event handlers.

    For the lookup it will be the best option to write lookup event handlers.

    Just expand the controls >> events >> copy the event handler for onLookup method.

    Create a new class >> paste the copid lookup event handler >> copy all the codes you have already written.

    You can refer to the blog mentioned by Mohit.

    Thanks,

    Girish S.

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

    Hello gentlemen,

    Thank you for your responses.  So far I'm having no luck.

    I created a new class and extended the field of the datasource, I put a break point on it, but I do not hit it when I click the dropdown of my field

    Here is the code

    [ExtensionOf(formDataFieldStr(InventNonConformanceTableCreate, InventNonConformanceTable, TCI_InventProblemSubTypeId))]
    public final class InventNonConformanceTableCreateTCI_InventProblemSubTypeIdTCI_Extension
    {
        public void lookup(FormControl _formControl, str _filterStr)
        {
            next lookup(_formControl, _filterStr);
    
            //Specify the name of the table the lookup should show data from.
            SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tableNum(TCI_InventProblemSubType), _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(TCI_InventProblemSubType));
            queryBuildRange = queryBuildDataSource.addRange(fieldNum(TCI_InventProblemSubType,ProblemTypeId));
            queryBuildRange.value(queryValue(element.InventNonConformanceTable.InventTestProblemTypeId));
            //queryBuildRange = queryBuildDataSource.addRange(fieldNum(AMInventItemMinorGroup,MajorGroupId));
            //queryBuildRange.value(queryValue(TCI_ItemTable.MajorGroup));
    
            //Specify which fields should be shown  in the lookup form.
            //  field returned is the first field referenced
            sysTableLookup.addLookupfield(fieldNum(TCI_InventProblemSubType, Description));
    
            sysTableLookup.parmQuery(query);
            sysTableLookup.performFormLookup();
        }
    }

    So then I went to my form and found my field  (which is in a field group)

    I opened the events, right-clicked on OnLookup and copied Event Handler Method

    pastedimage1676643426028v1.png

    I then created a new class and pasted in the event handler, but as soon as I build, it tells me that CreateInformation_TCI_InventProblemSubTypeId is not part of InventNonConformanceTableCreate.

    I tried changing that to just TCI_InventProblemSubTypeId, but same error, that it can't be found.

    pastedimage1676643526660v2.png

    Not sure where to go from here.

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: Overwrite lookup method on form datasource field

    It will be better to go with lookup event handler instead of the COC for control.

    Can you show us the code for lookup event handler so far you written.

    Thanks,

    Girish S.

  • Andrew Huisman Profile Picture
    313 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

  • GirishS Profile Picture
    27,825 Moderator 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
    313 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
    27,825 Moderator 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
    313 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?

  • Verified answer
    GirishS Profile Picture
    27,825 Moderator 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.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier 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,882

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 525 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans