Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Event on form close

Posted on by 6,603

Happy Friday! I know some of us, myself included, have checked mentally and have already started the weekend but I thought I would risk the question.

I need to add a modification to the WMSBillOfLadingCarrier form in order to trigger custom code to recalculate total weight for given a BOL number in order to update WMSBillOfLadingOrder. Should I override the finalize or close methods on the form? In which case I'm uncertain if doing so will bypass some current function. Or should I override the validateWrite method on WMSBillOfLadingCarrier?

I'm wondering what best practices are for this situation, and what other recommendations may there be?

Thanks very much!

  • Denis Macchinetti Profile Picture
    Denis Macchinetti 16,444 on at
    RE: Event on form close

    Hi

    As per my understanding, you have to update the total weight on WMSBillOfLadingOrder table as the total weight from the WMSBillOfLadingCarrier by BOL Id.

    If this is the requirement, you can simple run the example code on my above post on CloseOk method.

    I don't think is necessary to track the modified records.

    Regards

  • bankk Profile Picture
    bankk 6,603 on at
    RE: Event on form close

    Hi, I think the one thing which stood out as a question for me was how I would have to track the BOL's which have been modified. On the BOL carrier screen I can modify multiple BOL lines prior to closing the form. I'd have to track the list and access it from the CloseOK(). With this in mind, it made more sense to let each line update and trigger the recalc code. I'd like to hear your thoughts on the logic. Thanks in advance.

  • Suggested answer
    Denis Macchinetti Profile Picture
    Denis Macchinetti 16,444 on at
    RE: Event on form close

    Hi

    The right place in my opinion is in the CloseOk Form Method.

    So override the CloseOk form method and move your code inside, like :

    public void closeOk()

    {

       WMSBillOfLadingCarrier  wmsBillOfLadingCarrier;

       WMSBillOfLadingOrder    wmsBillOfLadingOrder;

       super();

       select sum(Weight) from wmsBillOfLadingCarrier

           where wmsBillOfLadingCarrier.billOfLadingId == "xxx";

       ttsBegin;

       select forUpdate wmsBillOfLadingOrder

       where wmsBillOfLadingOrder.billOfLadingId == "xxx";

       if ( wmsBillOfLadingOrder )

       {

           wmsBillOfLadingOrder.weight = wmsBillOfLadingCarrier.weight;

           wmsBillOfLadingOrder.update();

       }

       ttsCommit;    

    }

    In this way you don't have any issue.

    Regards

  • bankk Profile Picture
    bankk 6,603 on at
    RE: Event on form close

    Hi Denis,

    Sorry for the confusion. I have a simple piece of code which goes through the WMSBillOfLadingCarrier table given a BOL Id and sums up the weight value. I've put the code into the validateWrite() after the call to super(). So here's the series of events.

    1. On the BOL carrier form I update the weight on a line, I click close. The code runs and cycles through the line items and aggregates the weight.

    2. When I go to the WMSBillOfLadingCarrier table, I see the new line item

    3. When i go to the WMSBillOfLadingOrder table, I see the weight total, but it does not include the change I made in #1.

    4. Now if I make another change to weight and close the form, the code runs again, but this time, I will get a aggregated weight value in the WMSBillOfLadingOrder table which includes the change from #1 but not the change I just made in #4.

    Hope that makes sense.

    I worked around it and got something working. I'm retrieving the current weight value of the carrier line that's being changed, and then when I iterate through the WMSBillOfLadingCarrier table I ignore the line which has the same RecId. Seems a bit of a cludge but it works.

    Thank you for your help.

  • Denis Macchinetti Profile Picture
    Denis Macchinetti 16,444 on at
    RE: Event on form close

    Hi

    Can you move your code on update Table method after the Super()?

    Honestly, is not clear for me when do you say : "found that the commit is not called so when I recalculate the weight to update WMSBillOfLadingOrder i'm evaluating the old value".

    Can you move here your custom code?

    Regards

  • bankk Profile Picture
    bankk 6,603 on at
    RE: Event on form close

    Thank you everyone for your suggestions. I settled on validateWrite() on WMSBillOfLadingCarrier table but quickly found that the commit is not called so when I recalculate the weight to update WMSBillOfLadingOrder i'm evaluating the old value. I did not find any table method which is triggered after commit. Am I relegated to using a form method like finalize()?

    Thank you again.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Event on form close

    Place to Write Code

    Form level

    When code is related to whole form

    When code is related to multiple data sources

    Editor or Display methods (only those that are not related to any data source)

    Data source

    Data source-related Edit or Display methods

    Code related only to the data source that cannot be effectively placed in a table method

    Controls

    When it is strictly related to the controls

    for detailed reference following link

    www.packtpub.com/.../technical-best-practices-for-dynamics-ax-shared-and-aot-object-standards

  • Verified answer
    Denis Macchinetti Profile Picture
    Denis Macchinetti 16,444 on at
    RE: Event on form close

    Hi

    1- If you want trigger a Field modification, you can move your custom code on "modifiedField" or "ValidateField" of the WMSBillOfLadingCarrier table

    2- If you want trigger a Record update, you can move your custom code on ValidateWrite or Update table method. You can use ValidateWrite if you want handle only update made by a form.

    3- If you want to trigger the Form closing, you can move your custom code on CloseOk form method.

    About option 1 or 2, if you are using AX 2012 you can handle your custom code using Event Handler Nodes. This is the best solution reduce the upgrade effort to the next release.

    msdn.microsoft.com/.../gg839762.aspx

    Regards

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Event on form close

    Call the custom code from the table ModifiedField() method

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans