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 :
Finance | Project Operations, Human Resources, ...
Answered

adding methods to dynamically added fields [D365]

(0) ShareShare
ReportReport
Posted on by 10

Table A has a bunch of fields of various types organized in field groups.

Now I want to create a form that has those same groups and within them a checkbox for every field therein.

Table A is sometimes redesigned (new fields or groups added).  I don't want to have to redesign the checkbox form whenever this happens.

Using DictTable/DictFieldGroup/DictField I can loop over the current set of fields and use addControl() to dynamically build the groups and checkboxes.

Is it possible to make it so that when one of these checkboxes is toggled by the user,  a method is called with the name and new state of the checkbox?

Perhaps by making them send an onclicked event?

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    301,171 Super User 2025 Season 2 on at

    Hi drhex 365,

    Can you explain the business requirement? What is the purpose here? Why is a table sometimes re-designed? Do you want to display the fields and also a checkbox, or only checkboxes? Are these checkboxes bound to a table field or unbound controls? What type of method do you want to call? A generic one or a "dynamically added method" specific for that single checkbox?

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi,

    Usually you should not place any logic on form level, it's best practice to keep all logic on table level or in classes. In this case you can re-use logic in many places.

    I suggest that you don't need such kind of method for form, but you can use modifedField or ModifiedFieldValue on table level. If you need to change state of controls on the form, you can access datasource property from ModifiedField* methods as well to do this.

  • drhex_365 Profile Picture
    10 on at

    My form displays only checkboxes which are unbound controls, i.e. not fields in any table.

    A single generic method for all checkboxes would be best (as long as it gets some form of id for which checkbox was clicked).  The business requirements is that the fields selected by clicking their corresponding checkbox are to be part of a data export. The customer sometimes comes up with new stuff to keep track of which leads to fields being added to the underlying table.

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

    FormCheckBoxControl indeed has the OnClicked event that you can subscribe to in X :

    checkbox.OnClicked  = eventHandler(...);

    You can use the same handler methods for all checkboxes; you'll receive the actual checkbox object as the first argument.

  • Suggested answer
    drhex_365 Profile Picture
    10 on at

    Thanks Martin.  I had not worked with eventhandlers added from code before and so had to experiment a bit before getting it to work. Some things learned:

    1)  In the call to eventHandler(), you do not need to mention whatever arguments the event handler method has
    2) The method refered to does not necessarily need to be put in a class, it can be in a form as well
    3) When added this way,  the event handler method does not need to be prefixed with something like

    [FormControlEventHandler(formControlStr(MyFormName, Fieldname), FormControlEventType::Clicked)]

    that you usually get when using "copy event handler method" in the gui form designer

    So finally some more fleshed out example code:

    dynamic field creation in a form:

    FormCheckBoxControl  fc = groupcontrol.addControl(FormControlType::CheckBox, 'CheckboxName');
    fc.Onclicked += eventhandler(element.CheckBox_OnClicked);

    and the corresponding onclicked-method, which can be resused from multiple checkboxes:

    public void CheckBox_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormCheckBoxControl fc = sender as FormCheckBoxControl;
        if (fc)
        {
            info(strFmt("Checkbox %1 set to %2", fc.name(), fc.value()));
        }
    }

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

    The event handler attribute is another way for registering event handler methods. It's used at design time, but since you add controls at runtime, you can't use that.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 422 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans