Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

New method on form datasource by extension not working

(0) ShareShare
ReportReport
Posted on by 1,665

Hi everyone

I've implemented a new method on a form datasource by extension (on the SalesLine datasource of the SalesTable form to be exact).

When I call the method however, I get this error message: 

"Error executing code: FormDataSource object does not have method "xxx""

I couldn't find much documentation on how to do it, but eventually found these:

https://community.dynamics.com/365/financeandoperations/f/dynamics-365-for-finance-and-operations-forum/305905/calling-a-form-datasource-method-in-d365

https://community.dynamics.com/365/financeandoperations/f/dynamics-365-for-finance-and-operations-forum/291411/how-to-use-otb-custom-form-datasource-methods-in-extension-class-in-d365fo

I understand the principle of casting a FormDataSource object to a more generic/super object. Yet when I implement it, I get an error. Here is my implementation:

[ExtensionOf(formDataSourceStr(SalesTable, SalesLine))]

final class SalesTableDSSalesLine_Extension

{

        public void blah(boolean _variable)

        {

               // do something

        }

}

and the call:

[FormDataFieldEventHandler(formDataFieldStr(SalesTable, SalesLine, ItemId), FormDataFieldEventType::Modified)]

public static void ItemId_OnModified(FormDataObject sender,  FormDataFieldEventArgs e)

{

        Object dsObject = sender.datasource();

        dsObject.blah(true);

}

The only difference I can see is that other implementations are calling this from a form control, and not a form data field control, but surely that makes no difference.

The error message seems to indicate that the cast isn't happening properly: It's calling the method off an 'Object' object, but it still says that the method doesn't exist for 'FormDataSource' objects - strange.

What am I doing wrong?

Any help appreciated.

  • WillWU Profile Picture
    22,342 on at
    RE: New method on form datasource by extension not working

    Hi Brandon,

    Sorry the problem cannot be reproduced in my environment.

    As above said,  if the method has to be reused, you could put it to another place.

  • Martin Dráb Profile Picture
    225,155 Moderator on at
    RE: New method on form datasource by extension not working

    Duplicating code would indeed be wrong, but what prevents you to put the code to a separate method that you'll call where needed? It's what you're doing with blah(), you're just trying to place to a place where it's difficult to call. Would a class method does the same job while being easy to call?

    Maybe the hack with Object doesn't work with methods defined in extensions. If you wish, you can test it - create a custom form, define a data source method and call it from a handler. Then remove the method, define it through an extension and run the solution again.

  • Sergei Minozhenko Profile Picture
    23,083 on at
    RE: New method on form datasource by extension not working

    Hi BrandonSA,

    In examples you have provided, standard methods are used, but you are adding new method via CoC. It could be that this feature could be broken or never was implemented.

    Have you considered to move your code from data source lelve to table level for reusability (if possible) or at least to form level?

  • BrandonSA Profile Picture
    1,665 on at
    RE: New method on form datasource by extension not working

    Thanks Will. I could put all the blah functionality in the event handler, but then I wouldn't have a resuable method on the datasource. I would have to copy the code of blah to every place that needed to use it.

    Besides, I don't understand why my code isn't working. I've implemented it in the same way as the examples I gave, and those people say their code works, so just wondering why mine isn't.

    Thanks

  • BrandonSA Profile Picture
    1,665 on at
    RE: New method on form datasource by extension not working

    Thanks Blue, but it is being implemented on DataSource level

  • WillWU Profile Picture
    22,342 on at
    RE: New method on form datasource by extension not working

    Hi Brandon,

    Try to change the code in your form data source field event handler.

    FormRun   formRun = sender.datasource().formRun();
    
    Object salesLine_ds=formRun.dataSource(formDataSourceStr(SalesTable,SalesLine));
    
    salesLine_ds.blah(true);

    Actually you could write your 'blah' method in the event handler, why you have to create the new one in datasource?

  • Blue Wang Profile Picture
    on at
    RE: New method on form datasource by extension not working

    Hi BrandonSA,

    Try to write code at the data source level instead of the form level.

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

Dynamics 365 Community Update – Sep 16th

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

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... 283,860 Test role Public

#2
Ludwig Reinhard Profile Picture

Ludwig Reinhard Microsoft Employee

#3
Martin Dráb Profile Picture

Martin Dráb 225,155 Moderator

Leaderboard

Product updates

Dynamics 365 release plans