Skip to main content
Dynamics 365 Community / Forums / Finance forum / Calling a form datasou...
Finance forum
Answered

Calling a form datasource method in d365

editSubscribe (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.  

  • Kauto Profile Picture
    Kauto 2,641 on at
    Calling a form datasource method in d365
    Hi All,
     
    I have read this post cover to cover and I will explain my scenario - hopefully someone can assist.
     
    I have written an extension to the TsTimesheetEntry form.
     
    The extension is a simple check box added to the form that when turned on, the modified event handler method attempts to call a custom data method which has been added as an extension on the TsTimesheetTable form datasource.
     
    The idea is that if the form control is marked to true, this custom method on the form data source should be called to alter the query on the form and refresh.
     
    The issue I am having is similar to others in this blog but no matter which combination of the solutions I try, when I attempt to call the custom data method from the form data source in my form control event handler the system complains that the method cannot be found:  

    Error executing code: FormDataSource object does not have method 'delegateQuery'.
     
    But the custom method delegateQuery has been added via a form extension CoC method 
     
    [ExtensionOf(formDataSourceStr(TsTimesheetEntry, TSTimesheetTable))]
     
    public void delegateQuery(NoYes _applyFilter)
    {
             // query logic changed
    }
     
    My question is - how can I successfully call this custom data method from another custom form control event handler method.
     
    Thanks in advance
  • bafrad Profile Picture
    bafrad 40 on at
    RE: Calling a form datasource method in d365

    I do not have permission to mark this as an answer but I did something similar to that, it's just unfortunate we can't call our extension methods.

  • Suggested answer
    GirishS Profile Picture
    GirishS 27,799 Super User on at
    RE: Calling a form datasource method in d365

    Hi bafrad,

    In that case you can create a new class and add the below code.

    Public static void enableDisableControl(FormRun _fRun)
    {
        FormDataSource fds = _frun.datasource(FormDatasourceStr(FormName, DatasourceName));
        fds.object(fieldnum(TableName, FieldName)).enable(false);
    }

    So you can call this class in any form related methods with form run as argument.

    Public int active()
    {
        int ret;
        ret = next active();
        YourClassName::enableDisableControl(this.formRun());
        return ret;
    }

  • bafrad Profile Picture
    bafrad 40 on at
    RE: Calling a form datasource method in d365

    Girish,

    You are correct.  I actually did mention that in my post.  But I have other instances where I need to reference it.  Those will be outside of the data source, like within a control or even a field on the data source.

    I found this post: community.dynamics.com/.../is-it-possible-to-access-private-variables-in-chain-of-command-method-without-using-system-reflection and I wonder if the ability to call Custom methods custom data sources extended on a standard form is possible.

    I am working on just creating a controller class to accomplish this because I need to reuse this method throughout the form, not just within the datasource.

  • Suggested answer
    GirishS Profile Picture
    GirishS 27,799 Super User on at
    RE: Calling a form datasource method in d365

    Hi bafrad,

    You have created new extension class for "TestRelatedData" form datasource. So write the COC for active method in the same extension class instead of adding event handler for active method. Try the below code.

    [ExtensionOf(formDataSourceStr(LogisticsPostalAddressGrid, TestRelatedData))]
    final class TestRelatedData_FormDataSource_Extension
    {
        public int active()
        {
            int ret;
            ret = next active();
            this.object(fieldNum(TestRelatedData, Field1)).enabled(true);
            return ret;
        }
    }

    Thanks,

    Girish S.

  • bafrad Profile Picture
    bafrad 40 on at
    RE: Calling a form datasource method in d365

    This is the event handler

    [FormDataSourceEventHandler(formDataSourceStr(LogisticsPostalAddressGrid, TestRelatedData), FormDataSourceEventType::Activated)]
        public static void InitSecurity(FormDataSource sender, FormDataSourceEventArgs e)
        {
            Object TestRelatedDataDS = sender;
            sender.DisableFields();
        }

    This is the actual extension on the standard form LogisticsPostalAddressGrid.  I have extended this form and added a data source.

    [ExtensionOf(formDataSourceStr(LogisticsPostalAddressGrid, TestRelatedData))]
    final class TestRelatedData_FormDataSource_Extension
    {
        public void DisableFields()
        {
            this.object(fieldNum(TestRelatedData, Field1)).enabled(true);
        }

    I do not get any compile errors using now, but get this: Error executing code: FormDataSource object does not have method 'DisableFields'.

    Now what I can do in this instance is just add a CoC method for active in the same class for the T estRelatedData_FormDataSource_Extension.  But there are other instances outside of this that I need to reference this method and I get that error.

  • GirishS Profile Picture
    GirishS 27,799 Super User on at
    RE: Calling a form datasource method in d365

    Hi bafrad,

    Can you add the screenshot of code you have written?

    Thanks,

    Girish S.

  • bafrad Profile Picture
    bafrad 40 on at
    RE: Calling a form datasource method in d365

    I'm curious if this method is still valid to solving this problem?  While it resolves compile errors, I then get errors stating the method does not exist on the data source during actual execution even though it is there in code (and compiles).

  • Martin Dráb Profile Picture
    Martin Dráb 222,562 Super User on at
    RE: Calling a form datasource method in d365

    If it's your own form (not a standard form), you can let it implement an interface. Therefore you'll create an interface (let's call it MyInterface) with the method you need, add "implements MyInterface" to the form declaration.

    When you want to call your method, you'll cast the form reference to the interface. Like this:

    MyInterface foo = formRun as MyInterface;
    if (foo)
    {
        foo.myMethod();
    }

  • PK Axapta Profile Picture
    PK Axapta 140 on at
    RE: Calling a form datasource method in d365

    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.

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 Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

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