Skip to main content
Dynamics 365 Community / Forums / Finance forum / Best practices when in...
Finance forum
Suggested answer

Best practices when initializing datasources

editSubscribe (0) ShareShare
ReportReport
Posted on by 75
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.
 
  • Andre Arnaud de Calavon Profile Picture
    Andre Arnaud de Cal... 283,030 Super User 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 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

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Sundar…

Industry experts answer burning questions directly from our amazing Community…

Enabling Copilot Case and Conversation…

Agents can easily recap an ongoing chat, transcribe a voice conversation…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,030 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,562 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans