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

Event Handler class to refresh a Form

(0) ShareShare
ReportReport
Posted on by

Hi guys,

If I create a class which is an Event Handler class for a table : OnIsert, how to make Form whereby I trigger the insertion, to refresh all it's field ?

So this is the class (header) looks like :

class MyCaseAssociationOnInsert

{

[DataEventHandler(tableStr(CaseAssociation), DataEventType::Inserted)]

public static void CaseAssociation_onInserted(Common sender, DataEventArgs e)

{

…… <some code>

}

So basically the table is CaseAssociation, and the Form I referring is Case form, inside there is Case Association. My purpose for this Event handler is, when user ADD Case Association, it will trigger some action / job, and in that part will also update some of the fields in the Case Detail it self, like some fields in General section above it.

72207.Capture.PNG

The Event Handler is running fine, only the information is not refresh, and I need to press Refresh button to make it updated. However with those parameter -> ( Common sender, DataEventArgs e ) --> I cannot see a possibility to reach the caller form data source to make it refresh automatically.

Kindly advice,

Thanks

I have the same question (0)
  • Blue Wang Profile Picture
    on at

    Hi WWwong,

    Have you used datasource.refresh() or datasource.research(true)?

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

    If you hook in the insert event of a table, you can't indeed reach the form that did the insertion. This is true also without event handlers.

    After all, the insert method of a table can be called from many places, not just the form. If you wish to add some logic specific to a form, you need to develop your code on that form (or CoC / event handler of that form).

  • Community Member Profile Picture
    on at

    Hi Blue,

    I thought so, but the object, like "sender" only give me a short option ->

    pastedimage1585723014436v1.png

    How exactly the code will looks like ?

    Thanks,

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

    In table event handlers, sender is of type "Common" as mentioned. It has no relation to some form that might be inserting data to the table.

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

    Hi Wwong,

    You can access FormDataSource from record level by code below, but as Nikolaos mentioned, in this case, it will be applied to all forms where this table is used. It's much safer to implement the same change on the form level.

    if (sender.isFormDataSource())
    {
        FormDataSource fds = sender.dataSource();
        fds.reread(); //or executeQuery or research(true)
    }

  • Community Member Profile Picture
    on at

    Hi Nikolaos,

    So if by any chance I'm still using that class to insert (and update the table) that actually the underlying data source of that form, I can create another Event Handler from the CaseDetail Form like you told me, right?

    I just tried it, by create a new class, like this :

    class MyCaseDetailCaseAssociationOnCreated

    {

       /// <summary>

       ///

       /// </summary>

       /// <param name="sender"></param>

       /// <param name="e"></param>

       [FormDataSourceEventHandler(formDataSourceStr(CaseDetail, CaseAssociation), FormDataSourceEventType::Created)]

       public static void CaseAssociation_OnCreated(FormDataSource sender, FormDataSourceEventArgs e)

       {

           sender.reread();  

           sender.refresh();

       }

    }

    But somehow, it doesn't work. Logically it should work, right?

    Thanks,

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

    The onCreated event is not corrected, since that fires when the record is created but not yet inserted.

    Try the onWritten event.

  • Community Member Profile Picture
    on at

    Btw, just realize. This CaseDetail form contains many Data Sources, including Case Association and CaseDetail Base.

    The new EventHander I created, which is now as suggested is OnWritten, the "Sender" parameter, does it connect to CaseAssociation table only ? Which mean if I tell to : Sender.Refresh() --> it will refresh Case Association only, not the rest of data source including CaseDetailBase ?

    and should I have 2 lines, reread() and refresh() ?

    I read many time about these refresh/reread/research but still not very clear and confident on how and which one or two I should use.

    Thanks,

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

    Now the discussions starts deviating from your original question.

    Anyway, if you develop event handler on form data source, the sender points to that data source.

    However via that sender you can access the FormRun and all other data sources, controls, methods etc of the form.

    Refreshing the main data source of the form should refresh all others automatically as well. You can easily test it yourself.

    And, you should call formDataSource.refresh(), followed by formDataSource.research(true) as instructed in the very first reply above :)

  • Community Member Profile Picture
    on at

    Actually no, I'm still persistence on how to refresh my form :)

    Only my bad is still using the existing table event handler and try the workaround by create form event handler as your suggested.

    Anyway solved it. As suggested, I went to FormRun first ->

    [FormDataSourceEventHandler(formDataSourceStr(CaseDetail, CaseAssociation), FormDataSourceEventType::Written)]

       public static void CaseAssociation_OnWritten(FormDataSource sender, FormDataSourceEventArgs e)

       {

           FormDataSource CaseDetailBase_ds = sender.formRun().datasource(formDataSourceStr(CaseDetail,CaseDetailBase));

           CaseDetailBase_ds.refresh();

           CaseDetailBase_ds.research(true);

       }

    Case closed.

    Thanks to you all.

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... 456 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans