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, ...
Suggested answer

override lookup method to get a certain result

(0) ShareShare
ReportReport
Posted on by 1,552

Hi,

There is a form called CaseCategorySetup, that shows the hierarchy of cases.
Let's say I have this:

Level1  -- that's category type

  • 1A
    • 11A
      • 111A
    • 12A
  • 1B
    • 11B

Level2

  • 2A
  • 2B

let's say I have a form control where it's value is 1A

I have another control, where I want to amend the lookup to get the following result, which is anything under 1A

so the lookup should return to me 11A,111A and 12A  -- how can I do that?

i did the following but it's only returning 11A and 12A, how can I get 111A and anything else that might be below 1A?

SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(CaseCategoryHierarchyDetail), _formStringControl);
sysTableLookup.addLookupfield(fieldNum(CaseCategoryHierarchyDetail, CaseCategory));
Query query = new Query();
QueryBuildDataSource queryBuildDataSource;
queryBuildDataSource = query.addDataSource(tableNum(CaseCategoryHierarchyDetail));

CaseCategoryHierarchyDetail caseCategoryHierarchyDetail;
CaseCategoryType caseCategoryType;
select firstonly RecId from caseCategoryHierarchyDetail where caseCategoryHierarchyDetail.CaseCategory == Control1.text()
    && caseCategoryHierarchyDetail.CategoryType == str2Enum(caseCategoryType,CategoryType.text());
//here control1 value is 1A and CategoryType value is Level1

queryBuildDataSource.addRange(fieldNum(CaseCategoryHierarchyDetail,ParentRecId)).value(queryValue(caseCategoryHierarchyDetail.RecId));

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

I have the same question (0)
  • Suggested answer
    Mohit Rampal Profile Picture
    12,563 Moderator on at
    RE: override lookup method to get a certain result

    Hi, Your query is to find only first child record, you need to loop all child and sub child records.

    Please check this article

    yasirnedian.blogspot.com/.../today-i-am-going-to-show-how-we-can-get.html

  • junior AX Profile Picture
    1,552 on at
    RE: override lookup method to get a certain result

    Hi Mohit,

    I looked at this example before but I didn't know what to do with my query, can you please help? is it doable in lookup?

  • Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at
    RE: override lookup method to get a certain result

    Can you check the Purchase orders form where when you select procurement category when creating a PO. There you will have similar functionality. You can implement the same here also.

  • junior AX Profile Picture
    1,552 on at
    RE: override lookup method to get a certain result

    Hi bharani,

    The procurement category is similar to the case category field that shows all tree view.

    My question was about lookup, which is to return part of the tree view by looking at a control value and then return in the lookup only what's below that control..and not full tree view

  • Mohit Rampal Profile Picture
    12,563 Moderator on at
    RE: override lookup method to get a certain result

    Yes, it's doable in lookup. You can create while select loop to find child and sub child records based on parentRecId field and add range on all found values.

  • junior AX Profile Picture
    1,552 on at
    RE: override lookup method to get a certain result

    Hi Mohit,

    Can you please help me with code?

    I feel it's going to be multiple loops i'm alittle stuck

    And i need the lookup to appear as tree view

  • Suggested answer
    Mohit Rampal Profile Picture
    12,563 Moderator on at
    RE: override lookup method to get a certain result

    I have written this runnable class for you and tested that all Child and Sub child categories are included for a parent category. You need to use the same logic in your lookup code by providing the ParentRecId and calling this recursive method.

    Multiple loops are not required, only one while loop should do the job. This will show parent node as well but if you want, you can skip it by adding one more condition in while loop.

    internal final class RunnableClass
    {
        /// 
        /// Class entry point. The system will call this method when a designated menu 
        /// is selected or when execution starts and this class is set as the startup class.
        /// 
        /// The specified arguments.
        public static void main(Args _args)
        {
            CaseCategoryHierarchyDetail caseCategoryHierarchyDetail;
    
            select firstonly caseCategoryHierarchyDetail
                where  caseCategoryHierarchyDetail.CaseCategory == "Test";
    
            if (caseCategoryHierarchyDetail.RecId)
            {
                RunnableClass::findChildAndSubChild(caseCategoryHierarchyDetail.ParentRecId);
            }
          
        }
    
        public static void findChildAndSubChild(RefRecId _ParentRefRecId)
        {
            CaseCategoryHierarchyDetail caseCategoryHierarchyDetail;
    
            while select caseCategoryHierarchyDetail
                where caseCategoryHierarchyDetail.parentRecId == _ParentRefRecId
            {
                //queryBuildDataSource.addRange(fieldNum(CaseCategoryHierarchyDetail,ParentRecId)).value(queryValue(caseCategoryHierarchyDetail.RecId));
    
                Info(strFmt("Case Category: %1", caseCategoryHierarchyDetail.CaseCategory));
    
                RunnableClass::findChildAndSubChild(caseCategoryHierarchyDetail.RecId);
            }
        }
    
    }

  • junior AX Profile Picture
    1,552 on at
    RE: override lookup method to get a certain result

    Hi Mohit,

    Mohit Rampal 

    it worked and the lookup showed the result correctly.

    However, I mentioned that I wanted it to look as tree view, so I created a new formReferenceGroupControl instead of the formString control, then overrided the lookup like this where replaced sysTableLookup with SysReferenceTableLookup  and replaced _formStringControl input parameter with _formReferenceGroupControl. But now I can't even see the drop down on the control to see the lookup -- what's wrong?
    Also is there any property I need to fill on the form control ?

    ACaseCategory.RegisterOverrideMethod(methodStr(FormReferenceGroupControl, lookup), formMethodStr(CaseDetailCreate, AOverrideCaseCategoryLookup));


     public void AOverrideCaseCategoryLookup(FormReferenceGroupControl _formReferenceGroupControl)
        {
            SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tableNum(CaseCategoryHierarchyDetail), _formReferenceGroupControl);
            sysTableLookup.addLookupfield(fieldNum(CaseCategoryHierarchyDetail, CaseCategory));
            
            Query query = new Query();
            QueryBuildDataSource queryBuildDataSource;
            queryBuildDataSource = query.addDataSource(tableNum(CaseCategoryHierarchyDetail));
            
            CaseCategoryHierarchyDetail caseCategoryHierarchyDetail;
            CaseCategoryType caseCategoryType;
    
            select firstonly RecId from caseCategoryHierarchyDetail 
                where caseCategoryHierarchyDetail.CaseCategory == Control1.text()
                   
            if (caseCategoryHierarchyDetail)
            {
                this.findCaseCategoryChildAndSubChild(caseCategoryHierarchyDetail.ParentRecId, queryBuildDataSource);
            }
    
            
            sysTableLookup.parmQuery(query);
            sysTableLookup.performFormLookup();
        }
        
        public void findCaseCategoryChildAndSubChild(RefRecId _ParentRefRecId, QueryBuildDataSource _queryBuildDataSource)
        {
            CaseCategoryHierarchyDetail caseCategoryHierarchyDetail;
    
            while select caseCategoryHierarchyDetail
                where caseCategoryHierarchyDetail.parentRecId == _ParentRefRecId
            {
                _queryBuildDataSource.addRange(fieldNum(CaseCategoryHierarchyDetail,ParentRecId)).value(queryValue(caseCategoryHierarchyDetail.RecId));
    
                
    
                this.findCaseCategoryChildAndSubChild(caseCategoryHierarchyDetail.RecId, _queryBuildDataSource);
            }
        }

  • Suggested answer
    Mohit Rampal Profile Picture
    12,563 Moderator on at
    RE: override lookup method to get a certain result

    Hi Check these articles for TreeView lookup.

    ax-d365.com/.../

    daxsunil.weebly.com/.../treeview-lookup-in-d365fo

    Please mark answer/s Verified that worked for you so someone can refer it in future.

  • junior AX Profile Picture
    1,552 on at
    RE: override lookup method to get a certain result

    Hi Mohit,

    I verified the answer, but can you please help me more in creating the tree lookup in this thread:

    community.dynamics.com/.../1342542

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 582 Most Valuable Professional

#3
CU05031448-0 Profile Picture

CU05031448-0 526

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans