Skip to main content

Notifications

Announcements

No record found.

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

How to dynamically display data in a form after selecting from a lookup

(1) ShareShare
ReportReport
Posted on by 16

I'm working on a form in Dynamics 365 Finance & Operations where I need to show an additional field (StakeholderNameDisplay) after selecting a value from a dropdown (lookup) field (RoleCode). The idea is to fetch and display related data (e.g., a stakeholder’s title) without saving this data to the database; it should be for display purposes only.

Scenario
Table: I have a table (TUStakeholderRoles) with fields RoleCode, reportToPosition, and title.
Form: I want to add a display field (StakeholderNameDisplay) that will show the title of the selected stakeholder based on RoleCode.
Behavior: Once I select a RoleCode from the dropdown, the form should display the stakeholder’s title in StakeholderNameDisplay. This title should not be saved back to the table; it’s for display only.
Attempted Solutions
I’ve tried a few approaches but haven’t been able to get it to work as expected. Here’s what I’ve tried so far:

Solution 1: Display Method on Table

I added a display method directly in the table (TUStakeholderRoles) to retrieve the title based on the RoleCode.


public display str StakeholderNameDisplay()
{
    TUStakeholderRoles holderRoles;

    select firstOnly title from holderRoles
        where holderRoles.reportToPosition == this.RoleCode;

    return holderRoles ? holderRoles.title : 'NA';
}
This method works on a per-row basis, but when I try to use it on the form, it doesn’t update dynamically after the selection in the dropdown.

Solution 2: Adding the Display Method as a Form Control

I tried to add StakeholderNameDisplay as a control on the form by dragging it from the data source to the form design. However, the display method does not appear in the data source node in the form designer, so I couldn’t directly add it this way.

Solution 3: Using an Unbound Control with Modified Method on Dropdown

Since the display method didn’t show up directly, I added an unbound control (StakeholderNameDisplayControl) on the form. My plan was to update this control with the title dynamically. In the modified method of the dropdown control, I attempted to set the control’s text with the result of the display method:


public void modified()
{
    StakeholderNameDisplayControl.text(this.StakeholderNameDisplay());
    super();
}
But this approach failed because StakeholderNameDisplayControl was not found in scope within the data source’s methods, even though it’s in the form design.

Solution 4: Refreshing the Data Source to Update the Display Method

In another attempt, I used the modified method on the dropdown to refresh the data source, hoping it would trigger the display method to update dynamically.


public boolean modified()
{
    boolean ret;

    ret = super();

    // Refresh the data source to update the display method field
    yourTable_ds.refresh();

    return ret;
}

This didn’t produce the desired outcome either, as the display field still did not update dynamically.

Request
I’d appreciate any guidance on how to achieve the following:

Select a value in the RoleCode dropdown.
Automatically display the related title in a form field without saving it to the database.
Ensure the field updates dynamically each time the dropdown value changes.
Any solutions or alternative approaches would be really helpful. Thank you in advance
Categories:
  • CU06110926-0 Profile Picture
    CU06110926-0 16 on at
    How to dynamically display data in a form after selecting from a lookup
    thanks for the replies 
    Layan ,( yes this needs in related table but i posted on this table to make question simple )

    and i have a related question to this post 

     
    Is there any way to represent this value in the same drop down field without save it on database to not override to the saved id 
    so in this example i want to have  a one field "reportToPosition"
     

     
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    How to dynamically display data in a form after selecting from a lookup
    I'm not sure why you're asking whether you can use my solution. Sure, you can, that's why I shared it with you. I tested it and it does work.
     
    You didn't respond to my question about caching, but caching is likely the reason why you're saying that "the display method doesn't trigger". The value gets calculated one and then stored in a cache; it's not recalculate it when you change RoleCode. You could disable caching, but it would have performance implications, therefore explicitly recalculating the value when needed (in modified() method of RoleCode data source field) is the best solution.
     
    Also, you should do it below super() in modified(), not above.
  • Layan Jwei Profile Picture
    Layan Jwei 7,349 Super User 2024 Season 2 on at
    How to dynamically display data in a form after selecting from a lookup
    If the stakeHolderRole table has title field. Then why you want to create a display method in the same table for the title?
    It doesn't make sense to me unless i'm missing sth
     
    I assume there should be another table with a form where you would like the title to appear.
    So in this case you create a display method in the new table that returns the title
     
     
    So let's assume i have a table with the following fields:
    Table1Id and Name
     
    Now i created a new table with the following fields:
    Table2Id Table1Id
     
    Where this Table2 has a relation with Table1 based on Table1Id
     
    Now if i create a new form with Table1 and Table2 as a datasource, where I still would like to show the name from Table1 without editing it, then in this case i can create a new display method in Table2
    return Table1::find(this.Table1Id).Name
     
    And now i can add this display method to the form in addition to Table1Id
    And now when i select a value from the drop down of Table1Id, then Name will be filled automatically 
  • CU06110926-0 Profile Picture
    CU06110926-0 16 on at
    How to dynamically display data in a form after selecting from a lookup
    thanks for your reply ,Martin
    the proplem is the display method doesn't trigger as well and i think is not related to cashing as not changed the value with modified 

        public void modified()
        {
            //StakeholderNameDisplayControl.text(this.StakeholderNameDisplay());
            // Refresh the data source to update the display method field
            TUStakeholderRoles_ds.refresh();
            super();
        }


    can i change 
    TUStakeholderRoles_ds.refresh();

    with something more specific as you mention in you reply

    tuStakeholderRoles_ds.cacheCalculateMethod(tableMethodStr(TUStakeholderRoles, stakeholderNameDisplay));

     or any related mothod


    -- and there any way to represent this value in the same drop down field without save it on database to not override to the saved id 
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    How to dynamically display data in a form after selecting from a lookup
    Using the display method on the table sounds like the best approach to me.

    It should work as you want by default, if no caching is involved. Haven't you, for example, decorated the method with SysClientCacheDataMethod attribute?

    The method makes a DB query, therefore caching it is a good idea, but then you need to explicitly recalculate the method instead of getting the cached value. Call cacheCalculateMethod() after changing RoleCode (in modified() method of the data source field). For example:
    tuStakeholderRoles_ds.cacheCalculateMethod(tableMethodStr(TUStakeholderRoles, stakeholderNameDisplay));
     
    (Moved from Dynamics AX forum.)

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!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans