Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

Best practices when initializing datasources

Posted on by 83
Hello,
 
This is a question about best practices in reference to the initialization of datasources in a form. In my case, I have made a form consisting of 3 datasources:
- datasource_1
- datasource_2
- datasource_3
 
The datasource_2 and datasource_3 have datasource_1 as Join Source. After making several tests, I have seen that in the run() of the form, after the super(), the form only has initialized the datasource_1. When datasource_2 and 3 have Link Type: delayed, their record is loaded in its active() method. On the other hand, if the Link Type is active, their record is loaded in the active() of datasource_1.
 
So far so good, now let's say that datasource_3 has no record related to datasource_1, i.e. datasource_3 is blank. In this case, I want to initialize this record with information from datasource_2.
 
There are several ways to solve the problem. For example, check in the active() or executeQuery() if there is a datasource_3 record, and initialize otherwise. Or even use initValue() and initialize the fields directly from the datasource_2 table (using find method), instead of datasource_2 (because datasource_2 is not loaded yet).
 
However, I am not sure which solution is considered best practice. In my opinion, the initValue() is the cleanest. I have seen in other forms that the active() is used a lot, but I think it leads to unnecessary overhead, since the active() is called quite often.
 
What do you think?
 
 
Thanks in advance,

Eisenberk.
 
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 290,900 Super User 2024 Season 2 on at
    Best practices when initializing datasources
    Hi Eisenberk,
     
    If you want to initialize values for a new record, then active() is not the correct method. Like mentioned by Kevin and yourself the initValue() method it the appropriate one. That method is triggered when creating a new record.
  • Suggested answer
    Kevin Xia Profile Picture
    Kevin Xia Microsoft Employee on at
    Best practices when initializing datasources
    Hi,
    The best practice in your case would be to use the initValue() method to initialize the record in datasource_3 from datasource_2 directly. This approach can help you ensure clean and efficient code, avoiding unnecessary overhead. Additionally, checking in the active() or executeQuery() if datasource_3 has a record and initializing it when there isn't one is also a reasonable solution.
    Best regards,
    Kevin

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,275 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans