web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

How can I get the previous changed value of field in X++

(0) ShareShare
ReportReport
Posted on by 113

Hi Experts,

I have a requirement . If user change the Sales Price in Sales Line then a Dialog open and user select the Reason Code .
After that My SalesLine data will get saved in Custom History Table.
So I need to save the OriginalPrice(Previous Value which got changed) and Updated Price(New Value) in History Table.


How Can I achive this -


I used - salesLine.orig().SalesPrice


But it is saving the Original Value of Sales Price field every time I update that field for a particular record. 

I have the same question (0)
  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    Hi, I think you will get orig value before table is updated. How about creating CoC on update method of SalesLine table and capturing the values before and after next call and update Custom History Table.

  • André Arnaud de Calavon Profile Picture
    303,381 Super User 2026 Season 1 on at

    Hi Harish,

    Can you be more precise what you mean with "update that field for a particular record."? What x++ coding did you use and on what place did you put that coding?

  • HARISH MEHRA Profile Picture
    113 on at

    Hi,
    Writing code in OnModified form control.

       [FormControlEventHandler(formControlStr(SalesTable, SalesLine_SalesPrice), FormControlEventType::Modified)]
        public static void SalesLine_SalesPrice_OnModified(FormControl sender, FormControlEventArgs e)
        
        {
            FormDataSource salesLine_ds = sender.formRun().dataSource("SalesLine");
            SalesLine      salesLine    = salesLine_ds.cursor();      
            ReasonMain_CAP reason_Cap;
            PriceChangeHistory_CAP priceChangeHistory;
            Dialog dialog;
            Dialog dlg = new Dialog('@CAP:DialogCaptionInDialogSalesPrice');
            DialogField dfReasonCode = dlg.addField(extendedTypeStr(ReasonCode_CAP),'ReasonCode');       
     
                if(dlg.run())
                if (dfReasonCode.value() != '' )
                {
                    ttsbegin;
                    dlg.closedOk();
                    priceChangeHistory.ReasonCode = dfReasonCode.value();
                    priceChangeHistory.CurrencyCode = salesLine.CurrencyCode;
                    priceChangeHistory.OrderNumber = salesLine.SalesId;
                    priceChangeHistory.ItemNumber = salesLine.ItemId;
                    priceChangeHistory.LineNum = salesLine.LineNum;
                    priceChangeHistory.OriginalPrice = salesLine.orig().SalesPrice;
                    priceChangeHistory.UpdatedPrice = salesLine.SalesPrice;
                    priceChangeHistory.ReferenceType = '@CAP:SalesOrder';
                    priceChangeHistory.CreateDateTime = today();
                    priceChangeHistory.RecIDSales = salesLine.RecId;
                    priceChangeHistory.TableIDSales = salesLine.TableId;
                    priceChangeHistory.RecIDPriceChange = priceChangeHistory.RecIDPriceChange   1 ;
                    priceChangeHistory.CreateBy =  curUserId();
                    priceChangeHistory.insert();
                    ttscommit;
                }
                else
                {
                    salesLine.SalesPrice = salesLine.orig().SalesPrice;
                    Error('Please fill the reason code');
                }

  • Martin Dráb Profile Picture
    238,782 Most Valuable Professional on at

    If I understand it correctly, you're getting the original value of the field (before you started changing the record), but you don't want it. You want the previous value of the field, regardless of saving.

    For example:

    1. You have a record with price 1 and start editing it.
    2. You can the price to two, which creates PriceChangeHistory record with OriginalPrice = 1 and UpdatedPrice = 2.
    3. Then you change the price to five, which creates a PriceChangeHistory record with OriginalPrice = 2 and UpdatedPrice = 5.
    4. Then you change your mind, press Esc and cancel the update.

    The result is:

    1. The update was canceled, therefore quantity is still 1.
    2. Two PriceChangeHistory records where created (for changes that were done in the form), namely 1->2 and 2->5. Use explicitly save them even if the sales line isn't saved.

    I wonder if this is really what you want. I would guess that your actual intention was saving information about price change of an order line that was actually saved.

  • HARISH MEHRA Profile Picture
    113 on at

    Hi Martin,

    Yes as per this scenario I want the record to save in PriceChangeHistory  Table like this scenario-

    1.You have a record with price 1 and start editing it.

    2.You change the price to two, which creates PriceChangeHistory record with OriginalPrice = 1 and UpdatedPrice = 2.

    3.Then you change the price to five, which creates a PriceChangeHistory record with OriginalPrice = 2 and UpdatedPrice = 5.

    4.Again then OriginalPrice =5 and UpdatedPrice =9 and after updating price a dialog will open and If user cancel the dialog then no record is created on PriceChangeHistory Table and the Updated Value reverse back to OriginalPrice that is 5 in SalesPrice.

    But what is happening in my case is when I am using salesLine.orig().SalesPrice for storing the previous value, It is saving OriginalPrice as 1 all the time.

  • Martin Dráb Profile Picture
    238,782 Most Valuable Professional on at

    I understand what you're saying, but it's missing my point.

    Could you please confirm that your PriceChangeHistory table should store price changes that were never saved to SalesLine table? It's how your current implementation works, but it sounds strange. I suspect it's a bug in your overall design.

    Yes, the original price is 1 in all cases. With your current design, you want the previous values of the field of the buffer, not the original values with which the record was loaded from database. But it's possible that the original value is what you'll need in the correct solution.

  • HARISH MEHRA Profile Picture
    113 on at

    Yes. PriceChangeHistory table will store the Price change which were never saved in SalesLine table. This PriceChangeHistory table is to store the reason for which price is changed and what is the price that is changed.

  • Martin Dráb Profile Picture
    238,782 Most Valuable Professional on at

    But if the change isn't saved, no change occurred. Therefore the history will store information about change (e.g. that the price was changed from 10 to 15 and why), although the price didn't change. The history would claim that price changed to 15, which is a lie - it's still 10 and it always was.

    What business problem is solved by a change history table with these misleading entries?

  • Raj Borad Profile Picture
    1,524 on at
    Hi,
     
    I just created the history table of Customer master in AX.
    I write the History table insert code in ValidateWrite() method of CustTable.
     
    It stores the old value using this.orig().Field and new value using this.Field
     
    Thank you,
    Raj D Borad

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 514 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 390

#3
Adis Profile Picture

Adis 266 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans