Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Form datasource range

Posted on by 55
Hi, 
 
I have a form that has some fields (location is one of them) and a grid to display inventory in that location. 
I have added the second datasource (table2) to join the original one (InvenTable) through form design and by code with a range to default location (based on the company).
 
/// <summary>
        ///
        /// </summary>
        public void init()
        {
            super();
            QueryBuildDataSource qbds;
            qbds = this.queryBuildDataSource();
            qbds.clearLinks();
            qbds.addLink(fieldNum(InventTable, ItemId), fieldNum(table2, ItemId));
            qbds.addRange(fieldnum(table2, InventLocationId)).value(queryValue(CompanyInfo::Find().DataArea == 'C1' ? 'VALUE1' : 'VALUE2'));
        }
 
The form allows end user to change customer account, which has default location in their profile. How can I
1. Get default location from the form and use it in the range instead of hard-coding?
2. Update range on the second datasource and reload it so a proper values in the grid are displayed?
 
If there's another way (I know I can do calculated column here but business wants a filter the column, which isn't allowed with calculated columns), please recommend.
 
Thank you in advance, Alex
  • Alex Nach Profile Picture
    Alex Nach 55 on at
    Form datasource range
    Hi Martin.
     
    Thank you. I did some research through looking at various methods I can override on the DS and then googling these methods and came to the same conclusion. I appreciate you point me to the right location and confirming my thoughts. Cheers, Alex
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 228,552 Most Valuable Professional on at
    Form datasource range
    1. init() is called when a form is being created; no data has been loaded at that point. And of course, the user couldn't not set a value in the form before the form was created and displayed.
    2. You can run yourTable_ds.executeQuery() when the account changes. Inside executeQuery(), above super(), you can change value of the query range. You may create a QueryBuildRange object in init(), store in in a form-level variable and merely change its value inside executeQuery().
  • Alex Nach Profile Picture
    Alex Nach 55 on at
    Form datasource range
    Hi Martin and Andre. 
    First of all, thank you for pointing out about AddLink.
     
    In regards to implementation, no problem, here's high level breakdown of the form.
    The form is used to display/set defaults for a particular customer (this data is used for a webstore after). Customer Account field is unbound control on the form that allows customer selection and gets several values from d365 customer profile, like default location. InventTable brings items assigned to that customer with available inventory quantities column. I build that quantity field as a calculated column at first but business wants to filter out 0 qty so I had to think of another way and add second datasource (Table2), which adds available quantities to the grid.
    Now, when different customer account is selected, default values change, including default customer location. At that time, the grid reloads with different items and should bring available quantities for the items based on the default customer location.
    I hope that helps.
     
    My issues are the following: 1. from init method I cannot see/access any form fields. When I tried to get default customer location and/or account number form fields they are empty.
    2. How do I rerun the form datasource when customer account is changed on the form and pass default location so an appropriate range is added to datasource?
     
    Thanks, Alex.
  • Martin Dráb Profile Picture
    Martin Dráb 228,552 Most Valuable Professional on at
    Form datasource range
    To answer how to "get default location from the form", we need to know more about the implementation. You said that users can change a customer account, but where it is? Is it a field of a form data source? Which one? Or is it an unbound control?
     
    It's off-topic, but if you add a relation between table2 and InventTable, you won't need addLink(). Then your code will be merely:
    public void init()
    {
        super();
    
        this.queryBuildDataSource().addRange(fieldNum(table2, InventLocationId))
            .value(queryValue(CompanyInfo::find().DataArea == 'C1' ? 'VALUE1' : 'VALUE2'));
    }
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 290,552 Super User 2024 Season 2 on at
    Form datasource range
    Hi Alex,
     
    You mentioned that you want to get rid of the hard coding. You also mention that the location is stored on the customer details? You can read the customer account number and use that value to find a customer record to get the default location.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,552 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans