Skip to main content

Notifications

Announcements

No record found.

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 100

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. 

  • Raj Borad Profile Picture
    607 on at
    How can I get the previous changed value of field in X++
    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
  • Martin Dráb Profile Picture
    231,939 Most Valuable Professional on at
    RE: How can I get the previous changed value of field in X++

    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?

  • HARISH MEHRA Profile Picture
    100 on at
    RE: How can I get the previous changed value of field in X++

    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
    231,939 Most Valuable Professional on at
    RE: How can I get the previous changed value of field in X++

    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
    100 on at
    RE: How can I get the previous changed value of field in X++

    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
    231,939 Most Valuable Professional on at
    RE: How can I get the previous changed value of field in X++

    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
    100 on at
    RE: How can I get the previous changed value of field in X++

    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');
                }

  • André Arnaud de Calavon Profile Picture
    293,274 Super User 2025 Season 1 on at
    RE: How can I get the previous changed value of field in X++

    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?

  • Mohit Rampal Profile Picture
    12,556 Moderator on at
    RE: How can I get the previous changed value of field in X++

    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.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,274 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,939 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans