web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

how to call "form datasource" method from "field datasource" method

(3) ShareShare
ReportReport
Posted on by 1,979
Hi,
 

i put a method on formdatasource level as below



but it seems I can't call this method from the field DataSource level? as I'm getting this error:
ClassDoesNotContainMethod: Class 'FormDataSource' does not contain a definition for method 'xxx' and no extension method '' accepting a first argument of type '' is found on any extension class.


i tried four ways but all failed

1. How to fix it?
 

2. In general which way of the 4 is best to get formdatasource using CoC from ExtensionOf(formDataFieldStr?

    2a. when do i use 'formRun' like this:

         FormRun formRun = element;
         FormDataSource table1_ds = formRun.dataSource(formDataSourceStr(Form1, Table1));



   2b. when do i use 'element' like this

         FormDataSource table1DataSource = element.dataSource(formDataSourceStr(Form1, Table1)) as FormDataSource;


  2c. when do i use 'this' like this

        FormDataSource table1_datasource = this.dataSource(); 


 2d. when do i use 'formDataObject' like this

       FormDataObject fdo = any2Object(this) as FormDataObject;
      FormDataSource fds = fdo.datasource();

Categories:
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    238,123 Most Valuable Professional on at
    There are more ways, such as element.Table1_ds, but this.dataSource() sounds the best to me if you want the data source of the form control.
    You can declare the variable as Object to bypass the compile-time control, which might fix the problem, but it's not a good design and you'll likely get a compiler warning about it.
     
    Personally, I would declare the method in a form extension (rather than a data source extension) and then call it simply like element.mySetField1Access().
  • .. Profile Picture
    1,979 on at
    yes i tried "object" instead of formdatasource and it worked but a BP warning appeared as u said

    1a. So it means it's not a best practice to call a datasource method from from a field datasource method? (I mean always field datasource methods need to call "form" methods or methods in the "field datasrouce" itself but not a "form datasource" method?)
     
    1b. but why put it in form level, if it's only used for one datasource?
     
    2. yes element.Table1_ds and this.dataSource() both worked, but yes i agree, using "this" sounds better as it will gurantee that it gets the field datasource, as if i use element.Table1_ds there might be a mistake where i write the wrong datasrouce
     
    3. In general  think i should always use element in CoC but maybe use formRun in event handlers? right?
     
    4. when would I need to use FormDataobject like this? in which case?
           FormDataObject fdo = any2Object(this) as FormDataObject;
          FormDataSource fds = fdo.datasource();
  • Martin Dráb Profile Picture
    238,123 Most Valuable Professional on at
    1a. Correct.
    1b. This thread is about the reason.
    2 Good.
    3. No, you shouldn't use element always. You should use it when you need it and it's possible. You must use it when you want to refer to the form from components that belongs to the form, such as data sources or controls. Event handlers (typically) don't have element variable, therefore it's not an option to consider.
    4. Let me quote what I wrote elsewhere:

    There is something wrong with this reference in form datasource field extensions.

    For example, I created an extension of CustGroup.Name in CustGroup form. The type of this seems to be FormDataObject - I see it in debugger and get it from classIdGet() and GetType().

    But I can't assign it to a variable with FormDataObject type. Compilation fails with Cannot implicitly convert from type '$FormDataField_CustGroup_Name' to type 'FormDataObject'.

    If you try this as FormDataObject, the compiler claims that this isn't a class.

    A workaround of assigning this to an Object variable and then to FormDataSource works. Therefore the object is really an instance of FormDataObject.

    (I reported this in the preview program forum in 2022. Unfortunately, nothing has changed.)

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 679 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 421 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 282 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans