Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

limited fields of data sources in form from query

Posted on by 28
Hi experts,

I don't want to execute SQL on fields that are not needed in a form.

In the query object, I set only the fields to be used, but when I set that query on the form and DBSync it, the Data Sources screen on the form shows all fields, and when I check the SQL executed in the FO, all fields are Select.
To improve performance, I would like to execute SQL in which only necessary fields are selected.
  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    limited fields of data sources in form from query
    The error means that you're trying to call a method in a variable that doesn't contain a reference to any object (= is null). The first thing to check is where the error was thrown from. This should tell you which variable was null. Then you can investigate why there is no object when you expected one.
  • Ko Yamazaki Profile Picture
    Ko Yamazaki 28 on at
    limited fields of data sources in form from query
    Hi Martin,

    If I try to implement it by the means suggested below, I got a error "Object reference not set to an instance of an object".
    What should I check?
    I want to put values of them.
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    limited fields of data sources in form from query
    When you add a data source for a form, a query is created for it, but there is no X++ code involved. These are two different things.
     
    You already know the answer for the case you're interested in, i.e. forms: all fields are selected by default. We also discussed ways how to change this default.
     
    The answer happen to be the same for X++. If you use a select statement without a field list, it'll select all fields. And if you use QueryBuildDataSource class, all fields are added by default as well.
     
    To instruct a QueryBuildDataSource to select just some fields, you can do something like this:
    QueryBuildFieldList fieldList = qbds.fields();
    fieldList.dynamic(false);
    fieldList.clearFieldList();
    fieldList.addField(fieldNum(MyTable, MyField));
  • Ko Yamazaki Profile Picture
    Ko Yamazaki 28 on at
    limited fields of data sources in form from query
    I mean, Is it the default that all fields are Selected when executing SQL in x++?
    In my case, through form.
     
    How about using selectionfield in form?

    I understand it has a risk, and will pay attention, but as developers, I need to make performance improvements.
  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    limited fields of data sources in form from query
    I'm sorry, but I don't understand the sentence "Is SQL execution on a form basically a specification of the movement to extract all items?".
     
    Another approach I can think of is manipulating the form query in code, which allows you (among many other things) to choose fields to be fetched. But as discussed, it's quite dangerous and you should avoid it in form where users can change the data.
  • Ko Yamazaki Profile Picture
    Ko Yamazaki 28 on at
    limited fields of data sources in form from query
    Hi Martin,
    Thank you for answering.
    Is SQL execution on a form basically a specification of the movement to extract all items?
    Or do you have any ideas on how to improve performance in these cases, whether by query, form, or other means?
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    limited fields of data sources in form from query
    You can set OnlyFetchActive property of the data source to Yes, but be very careful about it, because it has quite a few implications. For example, the system won't allow you delete records, because it wouldn't be able to run delete actions to ensure data consistency. Or there is a risk of bugs if any code is used, because it may refer to fields that don't have values fetched.

    It's intended for lookup form, not normal forms where users can manipulate the data.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans