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

In x++ calculate

(0) ShareShare
ReportReport
Posted on by 130

Hi guys,

my requirement is sum of amount in PurchReqLine <LineAmount> addto to in PurchReqTable so that i extended  PurchReqTable  then add my custom field 'Total amount'

i can write display method in this scenario and i want to maintain the record so that i wrote a OnmodifiedFeild below my code it is not working on it what did i mistake.

class PurchReqTable_EventHandler
{
    
    
    /// 
    ///
    /// 
    /// 
    /// 
    [DataEventHandler(tableStr(PurchReqTable), DataEventType::ModifiedField)]
    public static void PurchReqTable_onModifiedField(Common sender, DataEventArgs e)
    {
        ModifyFieldEventArgs me = e;
        FieldId fieldId = me.parmFieldId();
        PurchReqTable thisRecord = sender as PurchReqTable;
        PurchReqLine line;

        switch(fieldId)
        {
            case fieldNum(PurchReqTable, TotalAmount):
                thisRecord.TotalAmount = (Select sum(LineAmount) from PurchReqLine
                    where  PurchReqLine.PurchReqTable == thisRecord.RecId).LineAmount;
                break;
        }
       
    }

    /// 
    ///
    /// 
    /// 
    /// 
    [FormDataFieldEventHandler(formDataFieldStr(PurchReqTable, PurchReqTable, TotalAmount), FormDataFieldEventType::Modified)]
    public static void TotalAmount_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        PurchReqTable ThisRecord = sender.datasource().cursor();
        ThisRecord.modifiedField(fieldNum(PurchReqTable, TotalAmount));

    }

}

Thanks 

I have the same question (0)
  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi WayToAx,

    1. You need to operate with PurchReqLine in this case, but not with PurchReqTable

    2. Don't use modified event as it will cause amount update even if the user hasn't decided yet to save the record and value will not be fixed in DB (you will sum old value).

    I would recommend to use insert\update\delete methods on PurchReqLine (or related event) and recalculate amount for a header after super\next statement.

  • WaytoAx Profile Picture
    130 on at

    thanks for your quickly replay  Sergei Minozhenko sir,

    give us a small example in sum of PurchReqLine amount show to in Header in PurchReqtable.

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

    Hi WayToAx,

    Just create extension class for PurchReqLine table and add COC methods for insert\update\delete methods

    [ExtensionOf(tableStr(PurchReqLine))]
    public final class PurchReqLine_MyModel_Extension
    {
        public void insert()
        {
            ttsBegin;
            
            next();
            
            PurchReqTable purchReqTable = this.purchReqTable(true)
            
            purchReqTable.TotalAmount = (Select sum(LineAmount) from PurchReqLine
                        where  PurchReqLine.PurchReqTable == purchReqTable.RecId).LineAmount;
            purchReqTable.update();    
            
            ttsCommit
        }
    
    }

  • WaytoAx Profile Picture
    130 on at

    many thanks sir.

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

    The design is wrong for multiple reasons:

    • First of all, you take into account only a single form - you ignore all other ways how a field can change (X++ code, data entities). Forms are not the place where you should maintain consistency of the database.
    • OnModified/modifiedField() is not triggered every time when a field is changed. It's called when you change the value in a form, but that's all. The right place is when a line is being saved to database (or deleted), i.e. insert(), update() and delete(). Check out if there isn't already code for handling that.
    • You want to react to a change of PurchReqLine.LineAmount, don't you? But you're not doing it - you react to changes of PurchReqTable.TotalAmount, which makes no sense. You would immediately see it if you tried to debug your code.
    • Even if you run your code when line amount is changed, it's not saved yet, therefore your select statement will give you old values. Again - modifiedField() is a wrong place for your logic.
    • modifiedField() is called when the field value is changed, while your implementation looks like if the method was used when you want request recalculation. And you're doing it only if you change the total amount and throw away the value you've just set. It makes no sense.

    Think again if you want to maintain the extra field. Maintaining it requires extra code and will have negative impact on performance, therefore you should do it only if it'll give you strong benefits.

    If you decide you must do it, run you code when changed lines (inserted, updated, deleted) are written to database and make sure you make all changes in a single database transaction.

    And make sure you familiarize yourself with the debugger - it's the best tool you have for finding problems in your code.

  • WaytoAx Profile Picture
    130 on at

    Thanks for your valuable answer sir, also i tried as a Sergei suggested code above it is not working on it and now i solved the problem in the display 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

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
Martin Dráb Profile Picture

Martin Dráb 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans