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

Notifications

Announcements

No record found.

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
    237,965 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
    237,965 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
    237,965 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans