Checking what changed in an event

John Sharp asked on November 15, 2017 12:09

In an Insert or Update event is there a way to understand what field has changed? I'm trying to isolate a function to only being called when a particular field has been changed but I'm not sure how to check the before and after versions of the object.


OrderInfo.TYPEINFO.Events.Update.After += InsertOrUpdate_After;

private void InsertOrUpdate_After(object sender, ObjectEventArgs e) { OrderInfo order = e.Object as OrderInfo; if (FieldChanged(order, OrderStatusID)) { // do my action; } }

Many thanks,


Correct Answer

Brenden Kehren answered on November 15, 2017 14:35

In your global event handler you can check the new value and original values using this:

var currentValue = e.Object.GetValue("ColumnName");
var originalValue = e.Object.GetOriginalValue("ColumnName");
1 votesVote for this answer Unmark Correct answer

Recent Answers

John Sharp answered on November 16, 2017 12:21

Thanks Brenden, I've added the code in and will send some vote happiness your way when I've had a chance to test.

0 votesVote for this answer Mark as a Correct answer

John Sharp answered on November 21, 2017 12:34

Thanks for the advice Brenden, I think I'm doing something wrong but you definitely helped with the final solution, so you deserve some Kentico happiness.

In the before event I'm adding the "value before change" to the OrderInfo custom data field and looking at it in the after event then removing it.

It looks almost like the system is committing/loading the values from the DB and in the on-load event the OriginalValue is set, by the time the after event has fired the values are appearing to be always equal.

Cheers though for the assist.


0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.