web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Custom Lookup on a reference group

(0) ShareShare
ReportReport
Posted on by 390

Hey team,

On the smmOpportunitytable form when I click new( ) option I have a custom form to create opportunities. The form has a custom field "Profit center" which is reference group but this reference group has no data source. It is extended from a customize EDT named "XYZ_ProfitCenter" which has table relation as:

0842.EDT.png

Form:

OppForm.png

I have created another table named "Retired Profit centers" now my issue is that I want to customize a lookup on this (Profit center) reference group and not include all the profit center which are in the "Retired Profit centers" table. As the data source is not specified for this reference group I cannot use "Lookupreference" method on the datasource. I have written the below code by overriding the lookup method on the form field but it is not working. Please let me know how can I create a custom lookup on the reference group in this scenario".

 [Control("ReferenceGroup")]
    class ProfitCentre
    {
        /// 
        ///
        /// 
        public void lookup()
        {
            super();
            RetiredProfitCenters retiredprofitcenter;
                OMOperatingUnit omoperatingunit;
                while select * from retiredprofitcenter
            {

                Query                   query = new Query();
                QueryBuildDataSource    queryBuildDataSource;
                SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tableNum(OMOperatingUnit), this);
               
                sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit,OMOperatingUnitNumber));
                sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit,OMOperatingUnitType));

                queryBuildDataSource = query.addDataSource(tableNum(RetiredProfitCenters));

                queryBuildDataSource.addRange(fieldnum(RetiredProfitCenters,OMOperatingUnitNumber)).value(SysQuery::valueNot(retiredprofitcenter.OMOperatingUnitNumber));

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

        }

    }

I have the same question (0)
  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Why did you decide to override lookup() instead of lookupReference()? It doesn't look correct to me.

  • skd Profile Picture
    390 on at

    Hey Martin Dráb,

    I tried using the lookupReference() earlier but it was giving me some errors so I used lookup() but after that I again tried  lookupReference()  now but another issue I'm facing is that on the front end when I'm clicking on the lookup column it is breaking the connection and giving an error "Error 503. The service is unavailable." Is there something wrong with my code?

    [Control("ReferenceGroup")]
        class ProfitCentre
        {
            /// 
            ///
            /// 
            /// 
            public Common lookupReference()
            {
                Common ret;
            
                RetiredProfitCenters retiredprofitcenter;
                OMOperatingUnit omoperatingunit;
                while select * from retiredprofitcenter
                {
              
                    Query                   query = new Query();
                    QueryBuildDataSource    queryBuildDataSource;
                    SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tableNum(OMOperatingUnit), this);
                    // SysTableLookup          sysTableLookup = SysTableLookup::newParameters(tableNum(OMOperatingUnit), sender);
    
                    sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit,OMOperatingUnitNumber));
                    sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit,OMOperatingUnitType));
    
                    queryBuildDataSource = query.addDataSource(tableNum(RetiredProfitCenters));
    
                    queryBuildDataSource.addRange(fieldnum(RetiredProfitCenters,OMOperatingUnitNumber)).value(SysQuery::valueNot(retiredprofitcenter.OMOperatingUnitNumber));
    
                    sysTableLookup.parmQuery(query);
                   ret = sysTableLookup.performFormLookup();
                }
                return ret;
            }
    
        }

  • Verified answer
    Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Your code is trying to run multiple lookups (one for each row in RetiredProfitCenters table), which is clearly wrong.

    Also, your query fetches RetiredProfitCenters records, but you're trying to show fields from OMOperatingUnit. Which table do you actually want?

    If you want to show operation units which aren't defined in RetiredProfitCenters, you need a query for OMOperatingUnit with non exists join to RetiredProfitCenters:

    public Common lookupReference()
    {
    	Query query = new Query();
    	
    	QueryBuildDataSource opUnitDs = query.addDataSource(tableNum(OMOperatingUnit));
    	QueryBuildDataSource retiredDs = query.addDataSource(tableNum(RetiredProfitCenters));
    	retiredDs.joinMode(JoinMode::NotExistJoin);
    	// This will work if you have a relation between those tables.
    	// Add the relation if needed.
    	retiredDs.relations(true); 
    
    	SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tableNum(OMOperatingUnit), this);
    	sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit, OMOperatingUnitNumber));
    	sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit, OMOperatingUnitType));
    	sysTableLookup.parmQuery(query);
    
    	return sysTableLookup.performFormLookup();
    }

  • skd Profile Picture
    390 on at

    Hey Martin Dráb,

    I understood where I was going wrong , thank you so much.

    When I tried the above code I'm getting an error "Queries with multiple top level data sources cannot be applied to Forms." Why is this happening? Please let me know.

  • skd Profile Picture
    390 on at

    Hey Martin Dráb,

    I think I fixed it.

    I just made change in this line and it worked.

    	QueryBuildDataSource retiredDs = opUnitDs.addDataSource(tableNum(RetiredProfitCenters));

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Sorry, it was indeed a bug in my code.

  • skd Profile Picture
    390 on at

    Hey Martin Dráb,

    Thank you so much for your guidance.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 468 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 333 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans