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

How to filter out(Add range) in lookup which is using a view?

(0) ShareShare
ReportReport
Posted on by 204
I have created a view with different datasources and  Have added the code for look up, it works fine.
But when I try to add range and build the system. It throws error as object reference missing.

For ex:
               SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(LineView),_formControl);
                Query          query = new Query(queryStr(LineQuery));
                QueryBuildRange         queryRange;
                QueryBuildDataSource    queryBuildDataSource;
                
 
               sysTableLookup.addLookupfield(fieldnum(LineView, field0), true);
                sysTableLookup.addLookupfield(fieldnum(LineView, field1));
                sysTableLookup.addLookupfield(fieldnum(LineView, field2));  //These work fine
                 
                queryRange = queryBuildDataSource.addRange(fieldNum(LineView, field1));
                queryRange.value(queryValue(HeaderTable.field1)); //But if I use query range, it build successfully, but shows object reference  error, when we check the look up.

How can we filter by adding ranges in a view look up?
  • Waed Ayyad Profile Picture
    9,222 Super User 2026 Season 2 on at
    Hi Community User,
     
    Can you add your code on a comment in order to get formatted?
      
    I can see two issues:
    1- You don’t declare the QueryBuildDataSource:
    You should add querybuilddatasource =  query.addDataSource(tableNum(YourView));
     
    2- Did you add these two lines on the code at the end of the lookup method?
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();


     
    Thanks 
    Waed Ayyad

    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Hana Xue Profile Picture
    Microsoft Employee on at
    Hi,
    Missing object references usually occur when trying to use or access an object that is not properly initialized or does not exist at the time of reference. The two lines Waed mentioned are necessary in order to apply the modified query to the SysTableLookup object and then perform the lookup. And you can check out this blog to see if it helps: 
    www.linkedin.com
    Best Regards,
    Hana
  • RSX Profile Picture
    204 on at
    Hi,
    Yes I've used this already :
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup(); 

    My question was, can we add range using view in a look up?
    Am I missing something in these 2 lines:
    queryRange=queryBuildDataSource.addRange(fieldNum(LineView,field1);
    queryRange.value(queryValue(HeaderTable.field1));
     
  • Suggested answer
    Martin Dráb Profile Picture
    241,349 Most Valuable Professional on at
    As Waed mentioned, you've never assigned any value to queryBuildDataSource variable, therefore your code must throw an exception when trying to call queryBuildDataSource.addRange().
  • RSX Profile Picture
    204 on at
    As Waed mentioned, I have used: querybuilddatasource =  query.addDataSource(tableNum(YourView));

    when I click on the field look up, I'm getting this error now : "Queries with multiple top level data sources cannot be applied to Forms." Below is the code
                    SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(MyView),_formControl);
                    Query          query = new Query(queryStr(MyQuery));
                    QueryBuildRange         queryRange;
                    QueryBuildDataSource    queryBuildDataSource =  query.addDataSource(tableNum(MyView));
                    
            
                    sysTableLookup.addLookupfield(fieldnum(MyView, ItemId), true);
                    sysTableLookup.addLookupfield(fieldnum(MyView, BOMId));
                    sysTableLookup.addLookupfield(fieldnum(MyView, Name));
                     
                    queryRange = queryBuildDataSource.addRange(fieldNum(MyView, BOMId));
                    queryRange.value(queryValue(MyHeaderTable.BOMId));
            
                    sysTableLookup.parmQuery(query);
                    sysTableLookup.performFormLookup();
     
  • Martin Dráb Profile Picture
    241,349 Most Valuable Professional on at
    The error message is correct - your query is invalid.
     
    You have MyQuery, which surely have a data source, let's say Table1. And you added MyView as another data source to the root of the query. Now the query in query variable have two data sources at the root level - Table1 and MyView.
     
    If you have to add MyView in code, instead of adding it to MyQuery, you must join it to a data source of MyQuery instead of adding it directly to the query.
  • Verified answer
    Layan Jwei Profile Picture
    8,353 Super User 2026 Season 2 on at
    Hi Community User,

    if you want the lookup to be from MyView then why do you need MyQuery?
    Query    query = new Query(queryStr(MyQuery));

    I would say replace it with this this

    Query  query = new Query();

    and leave everything else as is. And let us know if it works.

    So it would look like this:
                    SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(MyView),_formControl);
                    Query          query          = new Query();
    
                    QueryBuildRange         queryRange;
                    QueryBuildDataSource    queryBuildDataSource =  query.addDataSource(tableNum(MyView));
                    
            
                    sysTableLookup.addLookupfield(fieldnum(MyView, ItemId), true);
                    sysTableLookup.addLookupfield(fieldnum(MyView, BOMId));
                    sysTableLookup.addLookupfield(fieldnum(MyView, Name));
                     
                    queryRange = queryBuildDataSource.addRange(fieldNum(MyView, BOMId));
                    queryRange.value(queryValue(MyHeaderTable.BOMId));
            
                    sysTableLookup.parmQuery(query);
                    sysTableLookup.performFormLookup();


    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Waed Ayyad Profile Picture
    9,222 Super User 2026 Season 2 on at
    Hi Community User,
     
    The first comment that was added by me helped you. So I think you should verify my answer, it’s about 50% resolved the issue. Please verify my answer in order to help other in case they face the same issue in the future.
     
     
    Thanks,
    Waed Ayyad

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

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

#1
Martin Dráb Profile Picture

Martin Dráb 511 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 426 Super User 2026 Season 2

#3
CU-1234529-002 Profile Picture

CU-1234529-002 159

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans