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 / Assigning a value to a...
Finance forum

Assigning a value to a control and executing edit method via code in a form

(0) ShareShare
ReportReport
Posted on by

I have 2 fields on a form, both are reals. Field1 is a value in a grid, EditField2 is an edit method in the footer of the form. The edit method has some standard AX logic that I need to execute, using the value in Field1.

The functionality I'm looking for is: When I enter a value in Field1, it populates the value in EditField2, and executes the logic in the corresponding method. Basically, I want it to act as if the same value as was entered into Field1 was entered into EditField2 and then the user hit tab or enter.

In the Modified event handler for Field1, I've done this:

[FormControlEventHandler(formControlStr(SalesReleaseOrderPicking, SalesLine_InventDeliverNow), FormControlEventType::Modified)]

    public static void SalesLine_Field1_OnModified(FormControl sender, FormControlEventArgs e)

    {

        real lclQty;

        FormRun element                                 = sender.formRun();

 FormRealControl Field1        = element.design(0).controlName("Field1");

        FormRealControl EditField2 = element.design(0).controlName("Control2");

        FormRun formRun = sender.formRun();

        FormDataSource fds = formRun.dataSource(formDataSourceStrSalesLine

                              SalesLine)) as FormDataSource;

        SalesLine lclSalesLine = sender.formRun().dataSource(3).cursor();

        lclQty = lclSalesLine.QtyField;

        if (lclQty)

        {

            EditField2.realValue(lclQty);

         **   

            

        }

    }

My question is: Where I've put "**" in the code sample, what command would simulate an Enter in EditField2, so that the edit method will execute without user intervention?

Thank you in advance!

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    I guess you could call the method if you want to execute it. Just like with any other method.

    lclSalesLine.yourEditMethod(true, localQty):

    Does that work?

  • Community Member Profile Picture
    on at

    It doesn't. The error is that the table "SalesLine does not contain a method for yourEditMethod, and no extension method 'yourEditMethod' accepting a first argument of type SalesLine is found on any extension class".

  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    Just to be sure: did you use the name of your edit method? "yourEditMethod" was just an illustration since I have no idea what is your edit method name. Or even table. So please use the actual names of your table and your edit method. Does it work then?

  • Community Member Profile Picture
    on at

    I used the actual names. It's not finding that method because it's not a table method, it's a method on the form itself. Do you know how to access the method on the form to populate a value?

    Thanks for your help!

  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    If you have a method called yourMethod in your form data source, and if you have your form data source in variable fds, you can simply call your method like this:

    fds.yourMethod();
  • Community Member Profile Picture
    on at

    fds also does not find it. The name of the edit method is 'reserveNow', in the SalesReleaseOrderPicking form. It is a method on the SalesLine data source in that form. This is the method definition on the standard form: public edit InventQty reserveNow(boolean _set, SalesLine  _salesLine, InventQty  _reserveNow)

    I tried:

    FormDataSource fds = formRun.dataSource(formDataSourceStr(SalesLine,

                                 SalesLine)) as FormDataSource;

    fds.reserveNow(true, lclSalesLine, lclQty);

    It still errors, saying that the data method can not be found.

    Again, thank you!

  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    What if you try this

    1) Add a new method to the form root level with form extension. 

    2) Make this method call the edit method of the data source. 

    3) Call your form level method from your event handler (formRun.myNewMethod())

  • Community Member Profile Picture
    on at

    I found it. When I changed the FormDataSourcce to Object, it works. Not sure why...

    Old:

    FormDataSource fds = formRun.dataSource(formDataSourceStr(SalesLine,

                                SalesLine)) as FormDataSource;

    fds.reserveNow(true, lclSalesLine, lclQty);

    New:

    Object fds = formRun.dataSource(formDataSourceStr(SalesLine,

                                SalesLine)) as FormDataSource;

    fds.reserveNow(true, lclSalesLine, lclQty);

  • Verified answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    That's true. The FormDataSource class doesn't have that method, and that's why the compiler gives you an error. But with Object class there is no such check. Instead your code will fail in runtime if you call something that doesn't exist.

    To prevent such runtime errors, you can use formHasMethod method to check if the called form has a method that you intend to use. Maybe there is formDataSourceHasMethod available too. I don't have a system at hand right now so I can't check.

  • TestBot Profile Picture
    950 on at

    Hi all,

    Here in the comments sections found that using object for formdatasource, u can able to access formdatasource edit method. 

    May I know how to fetch form edit method similarly.. 

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