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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / Calling a form datasou...
Finance forum

Calling a form datasource method in d365

(1) ShareShare
ReportReport
Posted on by 70

Hi All,

i need to call form datasource method in event handler method.

[FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Activated)]
public static void SalesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
{

salesline_ds = element.dataSource(formDataSourceStr(salestable, SalesLine)) as FormDataSource;
salesline = salesline_ds.cursor();

salesline_ds.mcrMarginPercentStr(tmpSalesLine));

}

i found the following error

Severity Code Description Project File Line Suppression State
Error ClassDoesNotContainMethod: Class 'FormDataSource' does not contain a definition for method 'mcrMarginPercentStr' and no extension method 'mcrMarginPercentStr' accepting a first argument of type 'FormDataSource' is found on any extension class.  

I have the same question (0)
  • Sukrut Parab Profile Picture
    71,710 Moderator on at

    Where you created method mcrMarginPercentStr? Is it really exist  on form data source salesLine ? If it exist , did you compile package where you created data source extension ?  Also sender is your formdatasource , you don't have to write extra line you wrote in the above code.

  • Verified answer
    Martin Dráb Profile Picture
    237,970 Most Valuable Professional on at

    You don't need your code for getting the data source object - you've already got it in the sender parameter.

    Nevertheless FormDataSource class indeed doesn't have such a method. You can use the following workaround to call methods defined on forms:

    Object dsAsObject = sender;
    dsAsObject.mcrMarginPercentStr(tmpSalesLine);

    Nevertheless it's a display method; it's not clear why you want to execute it from your code.

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Muneeb Umer ,

    Try this :

       [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Activated)]
       public static void SalesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
       {
           FormRun formRun= sender.formRun();
           Object salesline_ds = formRun.dataSource(formDataSourceStr(salestable, SalesLine)) as FormDataSource;
           SalesLine salesline = salesline_ds.cursor();
           salesline_ds.mcrMarginPercentStr(tmpSalesLine);
       }


    Best Regards

    Tom Cai

  • Martin Dráb Profile Picture
    237,970 Most Valuable Professional on at

    Guys, please use the </> button in the rich formatting view to paste source code; it makes it easier to read.

    And once more, this:

    FormRun formRun= sender.formRun();
    formRun.dataSource(formDataSourceStr(salestable, SalesLine)) as FormDataSource;

    gives you the same thing you already have in the sender variable, therefore it can be thrown away completely.

  • Muneeb Umer Profile Picture
    70 on at

    Thanks Tom,

    this works for me changing type Formdatasource to object will work.

  • Martin Dráb Profile Picture
    237,970 Most Valuable Professional on at

    Which is what I suggested before with my simpler solution. Doesn't it work for you?

  • Muneeb Umer Profile Picture
    70 on at

    Thanks Martin,

    That's also work for me and as you mentioned it is a more simpler solution.

    Thanks again.

  • Noakesey Profile Picture
    50 on at

    Is anyone hitting this BP deviation when accessing a datasource method from an event handler?

     

    "A late bound call is made to .... In source system (AX 2012) it is possible to dynamically call methods where the number and type of the parameters does not match with the method definition.  This is not supported in AX 7, where the number and types of parameters have to match. Even if the parameters do match, the late bound call is extremely expensive. Mitigation: Use a class or interface hierarchy to provide a type safe fast call, or use the IS and AS operators to cast to a known type before calling."

    Just wondering what the best practice approach is? 

    I'm trying to call editMarkTrans on the CustTransOpen datasource of the CustOpenTrans form from a button clicked event handler:

    e.g.

    [FormControlEventHandler(formControlStr(CustOpenTrans, MyButton), FormControlEventType::Clicked)]
    public static void MyButton_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormRun fr = sender.formRun() as FormRun;
        FormDataSource fds = fr.dataSource(formDataSourceStr(CustOpenTrans, CustTransOpen)) as FormDataSource;
        CustTransOpen custTransOpen = fds.getFirst() as CustTransOpen;
    Object obj = fds as Object; obj.EditMarkTrans(true, custTransOpen, NoYes::Yes); }

  • AX Techie Profile Picture
    995 on at

    Hi Paul,

    Is your problem solved. Please let me know.

    [quote user="Muneeb Umer"]

    Hi All,

    i need to call form datasource method in event handler method.

    [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Activated)]
    public static void SalesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
    {

    salesline_ds = element.dataSource(formDataSourceStr(salestable, SalesLine)) as FormDataSource;
    salesline = salesline_ds.cursor();

    salesline_ds.mcrMarginPercentStr(tmpSalesLine));

    }

    i found the following error

    Severity Code Description Project File Line Suppression State
    Error ClassDoesNotContainMethod: Class 'FormDataSource' does not contain a definition for method 'mcrMarginPercentStr' and no extension method 'mcrMarginPercentStr' accepting a first argument of type 'FormDataSource' is found on any extension class.  

    [/quote]

    Thanks.

  • PK Axapta Profile Picture
    140 on at

    Hi Paul,

    Could you please advise how you resolved this BP warning (Mitigation: Use a class or interface hierarchy to provide a type safe fast call, or use the IS and AS operators to cast to a known type before calling.)

    Any pointers would be highly appreciated.

    Many thanks in advance.

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 > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans