Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

override lookup method to get a certain result

(0) ShareShare
ReportReport
Posted on by 1,550

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();

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

    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.RecId, queryBuildDataSource);  //before i used to pass Parent now RecId
            }
    
            
            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,RecId)).value(queryValue(caseCategoryHierarchyDetail.RecId)); //before fieldnum was parent now it's RecId
    
                
    
                this.findCaseCategoryChildAndSubChild(caseCategoryHierarchyDetail.RecId, _queryBuildDataSource);
            }
        }


    fixed

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

    Hi mohit,

    I'm debugging it's like it's returning anything at the same level

    so if I'm at t   -- why it's returning   3,2,5,6  and not just 3,2 --- hence it didn't return 4 -- I'm not sure what I need to change in code



    As I said, the requirement was to return anything below the level chosen

    Mohit Rampal 

  • Mohit Rampal Profile Picture
    Mohit Rampal 12,554 Super User 2024 Season 1 on at
    RE: override lookup method to get a certain result

    Hi, You need to debug and make changes to code as per the requirement then. The code I provided is for reference and not use as it is. I believe I tested it passing value 5 (as per your screenshot) and got values 5 and 6.

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

    Hi Mohit,

    the recursive function you provided me is not working properly.

    In case I'm standing at level t  -- it's returning to me 3,2,5,6  --- however I said that I want it to return what's below the level I pass -- so it should only return 3 and 2 in this case



    it's only working correctly if I'm standing at level accident -- it returns t,3,2,4,5,6

    But if I'm standing at t, it's returning 3,2,5,6 but it should only be returning 3 and 2 

    Mohit Rampal 

  • junior AX Profile Picture
    junior AX 1,550 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

  • Suggested answer
    Mohit Rampal Profile Picture
    Mohit Rampal 12,554 Super User 2024 Season 1 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
    junior AX 1,550 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
    Mohit Rampal 12,554 Super User 2024 Season 1 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
    junior AX 1,550 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

  • Mohit Rampal Profile Picture
    Mohit Rampal 12,554 Super User 2024 Season 1 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.

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…

Vahid Ghafarpour – Community Spotlight

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

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,031 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,868 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans