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

Community site session details

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

New method on form datasource by extension not working

(1) ShareShare
ReportReport
Posted on by 1,673

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.

I have the same question (0)
  • Suggested answer
    Waed Ayyad Profile Picture
    8,874 Super User 2025 Season 2 on at
    New method on form datasource by extension not working
    Hi,
     
    Try to use the following code in order to get your DataSource:
    FormRun        formRun = sender.formRun();
    FormDataSource salesLine_ds = formRun.dataSource(formDataSourceStr(SalesTable,SalesLine));
    salesLine_ds .blah(true);
    Thanks,
    Waed Ayyad
    If this helped, please mark it as "Verified" for others facing the same issue
     
  • Suggested answer
    Sourav Mazumdar Profile Picture
    38 on at
    New method on form datasource by extension not working
    Hi BrandonSA,
     
    Please try the below approach inside form control event handler.
     
    FormRun formRun = sender.formRun();
    Object          Table_ds = formRun.dataSource(formDataSourceStr(Formname,Datasourcename));
     
  • AtulMP Profile Picture
    4 on at
    New method on form datasource by extension not working
    Hello,
    There are some suggestions you can try to solve your problem.
     
    1. Create an Extension Class: Define your method in an extension class for the data source.
    [ExtensionOf(formDataSourceStr(SalesTable, SalesLine))]
    final class SalesTableDSSalesLine_Extension
    {
        public void blah(boolean _variable)
        {
            next blah(_variable); // Call the base method if needed
            // Your custom logic here
        }
    }
     

    2. Use Chain of Command (CoC): Extend the method using CoC to ensure it integrates with the base method.

    [ExtensionOf(formDataSourceStr(SalesTable, SalesLine))]
    final class SalesTableDSSalesLine_Extension
    {
        public void blah(boolean _variable)
        {
            next blah(_variable); // Call the base method if needed
            // Your custom logic here
        }
    }
     

    3. Call the Extended Method: Cast the data source correctly and call the extended method.

    [FormDataFieldEventHandler(formDataFieldStr(SalesTable, SalesLine, ItemId), FormDataFieldEventType::Modified)]
    public static void ItemId_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        SalesLine ds = sender.data source() as SalesLine;
        if (ds)
        {
            SalesTableDSSalesLine_Extension extendedDs = ds as SalesTableDSSalesLine_Extension;
            if (extendedDs)
            {
                extendedDs.blah(true);
            }
        }
    }
     

    This approach ensures your custom method is recognised and executed correctly.

  • RAGU Veelead Profile Picture
    6 on at
    New method on form datasource by extension not working
    Hi BrandonSA,
     
    try to change the code in the form data source field - chain of command.
     
    [ExtensionOf(formDataFieldStr(SalesTable,SalesLine,ExternalItemId))]
    final class AMM_SalesLineExtItemId_Extension
    {
       
        void  modified()
        {
            SalesLine                  salesLine;
            FormDataObject      formdo =  any2Object(this) as FormDataObject;
            salesLine                  = formdo.datasource().cursor();
            next modified();
            info(salesLine.salesid);
        }
    }
     
  • WillWU Profile Picture
    22,359 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
    235,961 Most Valuable Professional 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,093 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,673 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,673 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,359 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?

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

Responsible AI policies

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

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 789 User Group Leader

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 497 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans