Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Suggested answer

Button Event Handler

Posted on by Microsoft Employee

Hi everyone,

I want to add some code in a button of a form. Its postJournalButton. I want to add the code ONLY if the posted is done succesfully. 

 /// 
    /// 
    /// 
    [FormControlEventHandler(formControlStr(ProdJournalTransProd, PostJournal), FormControlEventType::Clicked)]
    public static void PostJournal_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        
        FormRun         formRun = sender.formRun() as FormRun;
        FormDataSource  prodJournalProd_ds;
        ProdJournalProd prodJournalProd;
   
        
        prodJournalProd_ds  = sender.formRun().dataSource(2);   // form DataSource (el segundo en orden)
        prodJournalProd     = prodJournalProd_ds.cursor();      // Selected record

    IF (JOURNAL POSTED)
        MY CODE HERE
      

How can I achieve this?

Thanks.

  • Goldie Profile Picture
    Goldie 187 on at
    RE: Button Event Handler

    Hi Martha,

    Check posting class , you will find place where Info log "Journal posted successfully" or may be some other info log for posting.

    Run COC on this class method and check if Status is Posted and can write your logic there.

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,148 on at
    RE: Button Event Handler

    I suggest hooking into the posting class, not the form button. Otherwise your code will run only when the button is clicked, but not if posting happens in batch or is called from elsewhere.

    Neither approach modifies the standard code, both are extensions. Modifying the standard code is not possible.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Button Event Handler

    I everyone, thanks for all replies,

    Its D365FO, my requeriment its add some data to a table when the journal is posted (ONLY when the posted was successful) I don't want to modify the standar, only add little bit code.

    Sumit Loya, I will try your suggestion. Thanks a lot.

  • Suggested answer
    Sumit Loya Profile Picture
    Sumit Loya 2,230 on at
    RE: Button Event Handler

    I highly doubt the request is for AX 2012. The requester has posted code with following syntax for event handler

    [FormControlEventHandler(formControlStr(ProdJournalTransProd, PostJournal), FormControlEventType::Clicked)]

    I don't believe this syntax was possible in AX 2012.

  • Suggested answer
    Khushhal Garg Profile Picture
    Khushhal Garg 1,512 on at
    RE: Button Event Handler

    Looks like ask is for AX 2012 assuming post is uploaded in AX forum so CoC is not applicable there. CoC (Chain of Command) is in D365FO. If it is for D365FO then CoC can also be used.

  • Suggested answer
    Sumit Loya Profile Picture
    Sumit Loya 2,230 on at
    RE: Button Event Handler

    Hi Marta,

    The ideal way would be to create CoC of postJournal method instead of button. If your requirement is only for Button, then I would suggest you create an Extension class of PostJournal form control and then create CoC of clicked method. Call the next clicked() first and then write your code. This way you will have all standard code execute and only on successful completion, your code will execute.

  • Suggested answer
    Khushhal Garg Profile Picture
    Khushhal Garg 1,512 on at
    RE: Button Event Handler

    Look at method postJournal of class ProdJournalCheckPost and write post event handler on this method. IT should solve your problem

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,148 on at
    RE: Button Event Handler

    What is your business requirement?

    The event handler is triggered every time when the button is clicked. Why not create an event handler for the posting class/method instead?

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,989 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans