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!

  • Shaik146 Profile Picture
    936 on at
    RE: Assigning a value to a control and executing edit method via code in a form

    Haven't tried yet. Sure will let you know.

  • nmaenpaa Profile Picture
    101,156 Moderator on at
    RE: Assigning a value to a control and executing edit method via code in a form

    Did you try it? Please let us know how it went. Thanks!

  • Shaik146 Profile Picture
    936 on at
    RE: Assigning a value to a control and executing edit method via code in a form

    Hi, 

    Fetch in the sense I mean, how to call. 

    Ex: formcontrol myControl = sender;

    Object formrun = myControl.FormRun() ;

    Str myEditvalue = formRun.editPurchAgreementId();// example

    So here can I get the edit method value in my string variable.? 

  • nmaenpaa Profile Picture
    101,156 Moderator on at
    RE: Assigning a value to a control and executing edit method via code in a form

    What do you mean by "fetch"?

    You can call form methods if you have the FormRun object. But you don't want to call the method, you want to run your own checks when the method is being called.

  • Shaik146 Profile Picture
    936 on at
    RE: Assigning a value to a control and executing edit method via code in a form

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

  • Verified answer
    nmaenpaa Profile Picture
    101,156 Moderator on at
    RE: Assigning a value to a control and executing edit method via code in a form

    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.

  • Community Member Profile Picture
    on at
    RE: Assigning a value to a control and executing edit method via code in a form

    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);

  • Suggested answer
    nmaenpaa Profile Picture
    101,156 Moderator on at
    RE: Assigning a value to a control and executing edit method via code in a form

    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
    RE: Assigning a value to a control and executing edit method via code in a form

    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,156 Moderator on at
    RE: Assigning a value to a control and executing edit method via code in a form

    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();

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,999 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,831 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans