Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Update a AX Form data field that is bound to data source

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I need to set the value for ToDate to be the value of FromDate field when FromDate is selected by the user. both are FormDateControl controls. my code in fromDate.TextChange event handler is like toDate.DateValue(this.DateValue()), but it doesn't workout. from code degging the value of toDate is updated but it still shows the old value on the screen. In MSDN I found

"Note

This property (DateValue) is not used if the DataSource and DataField properties are set."

this means it doen't work to try to update the control, I need to update the datasource, which I can somehow get it from element.datasource() method. is this correct thinking? any advices are greatly appreciated.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update a AX Form data field that is bound to data source

    So, my GUI library generic functions ended as shown below. Please, feel free to suggest possible optimizations.

    public static anytype getFieldValue( FormControl ctrl, boolean trim = true )
    {
        if (ctrl is FormComboBoxControl)
        {
            FormComboBoxControl cb = ctrl as FormComboBoxControl;
            int value = cb.dataSourceObject().cursor().(cb.dataField());
            return value;
        }
        else if (ctrl is FormStringControl)
        {
            FormStringControl sc = ctrl as FormStringControl ;
            str value = sc.dataSourceObject().cursor().(sc.dataField());
            if (trim) value = strRTrim(strLTrim( value ));
            return value;
        }
        else
            return null;
    }

    public static void setFieldValue( FormControl ctrl, anytype value, boolean trim = true )
    {
        if (ctrl is FormComboBoxControl)
        {
            FormComboBoxControl cb = ctrl as FormComboBoxControl;
            cb.dataSourceObject().cursor().(cb.dataField()) = value;
        }
        else if (ctrl is FormStringControl)
        {
            FormStringControl sc = ctrl as FormStringControl ;
            if (trim) value = strRTrim(strLTrim( value ));
            sc.dataSourceObject().cursor().(sc.dataField()) = value;
        }
    }

    Unfortunately, I could not use generic FormControl type as dataSourceObject() and dataField() methods do not apply to all controls, neither overloaded functions per control type as it seems not to be supported in X++.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update a AX Form data field that is bound to data source

    Following your last hint I ended up with the following statement that works fine!

    • formControl.dataSourceObject().cursor().(formControl.dataField()) = newValue;

    Thanks again!

  • Martin Dráb Profile Picture
    Martin Dráb 230,354 Most Valuable Professional on at
    RE: Update a AX Form data field that is bound to data source

    You should start paying attention to data types. You seem to believe that myFormControl.dataSource() returns a table buffer, but you're wrong. You can see the actual return type in Visual Studio and in the documentation. It's again a number and numbers has no table fields, therefore your code makes no sense. It's also what the compiler must be telling you - listen to it.

    What you want to do is getting a reference to the form data source that the control is bound to (FormControl.dataSourceObject()) and then getting the table buffer from the data source (FormDataSource.cursor()).

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update a AX Form data field that is bound to data source

    Thank you, Martin; that worked fine!

    Last question is how to use the dataSource control property to determine myTable?

    I already tried the following 2 syntax, but without success:

    (myFormControl.dataSource()).(myFormControl.dataField()) = newValue;

    or 

    myFormControl.dataSource().(myFormControl.dataField()) = newValue;

  • Martin Dráb Profile Picture
    Martin Dráb 230,354 Most Valuable Professional on at
    RE: Update a AX Form data field that is bound to data source

    dataField is a method, not a field, therefore the right syntax for using it is myFormControl.dataField().

    It returns a field ID, which is an integer. Value types in X don't have methods, therefore trying to call text() method on the integer is not allowed at all. Also, no manipulation of the integer value can set a value to the table buffer. The number has no reference to any form buffer - it's just a value such as 5.

    But what you want is achievable. You can use special syntax for accessible buffer fields by IDs:

    myTable.(myFormControl.dataField()) = newValue;

    Obviously the control must be bound to the same table as the buffer.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update a AX Form data field that is bound to data source

    Hello,

    I understand the myTable.myField = newValue; concept to change a field value in the buffer, as described here.

    But what if we want to achieve the same result by using the form control dataField property? That would be useful for a GUI library or class manipulating the form controls directly.

    I tried the myFormControl.dataField.text(newValue); for a String field form-control, but it failed with Error: Invalid token "(". 

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update a AX Form data field that is bound to data source

    A summary note, following are the code I have put on the transToDate field's modified event. two points:

    • .refresh() helps to reflect the changes simultaneously, rather than until the user clicks somewhere outside
    • a message box to notify the user the changes is a user-friendly practice.

     

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update a AX Form data field that is bound to data source

    Thanks again to you Dick and Martin for the points, you helped me resolving the issue.

  • Martin Dráb Profile Picture
    Martin Dráb 230,354 Most Valuable Professional on at
    RE: Update a AX Form data field that is bound to data source

    Kevin, a record is saved automatically when you leave it, typically by switching to another record, but it also includes closing the form.

  • Dick Wenning Profile Picture
    Dick Wenning 8,705 on at
    RE: Update a AX Form data field that is bound to data source

    Marin,

    I always add those lines otherwise you get messages like, an other user has changed the date press F5 etc.

    also the form is not updated with the new transtodate.

    so performance yes, impact, but the user realy does not message like In my first line

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,622 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans