Finding all changes to an Entity using the IChangedState

A long time ago Ryan wrote about how to check for a specific change to a field in this blog post.

Today I wanted to show a more generic way of using the same functionality to show all changes made to an entity.

Lets look at how to do that using a method that accepts an input of an EntityBase record, so that it can be reused for any entity:

    protected void ShowAllChanges(Sage.SalesLogix.Orm.EntityBase _entity)
    {
        string changes = string.Empty;
        if (_entity != null)
        {
            System.ComponentModel.IChangeTracking tracking = (System.ComponentModel.IChangeTracking)_entity;
            if (tracking.IsChanged)
            {
                Sage.Platform.ChangeManagement.IChangedState state = _entity as Sage.Platform.ChangeManagement.IChangedState;
                if (state != null)
                {
                    Sage.Platform.ChangeManagement.PropertyChange[] change = state.GetChangedState().FindAll<Sage.Platform.ChangeManagement.PropertyChange>();
                    Sage.Platform.ChangeManagement.EntityPropertyChange[] entitychange = state.GetChangedState().FindAll<Sage.Platform.ChangeManagement.EntityPropertyChange>();
                    if (change != null)
                    {
                        for (int i = 0; i < change.Length; i++)
                        {
                            changes += string.Format("{0} changed from {1} to {2}{3}", change[i].MemberName, change[i].OldValue, change[i].NewValue, System.Environment.NewLine);
                        }
                    }
                    if (entitychange != null)
                    {
                        for (int i = 0; i < entitychange.Length; i++)
                        {
                            changes += string.Format("{0} changed from {1} to {2}{3}", entitychange[i].MemberName, entitychange[i].OldEntity.GetReferencedEntity(), entitychange[i].NewEntity.GetReferencedEntity(), System.Environment.NewLine);
                        }
                    }

                }
                //now do something with the value in the variable changes
                throw new Sage.Platform.Application.ValidationException(changes);
            }
        }
    }

There you go! This type of code could be implemented in an entity’s OnBeforeUpdate event to gather all changes into a single message, like if you wanted to email this data all at once.

To call this, you simply would need to take you Interface and convert it to the EntityBase like this:

Sage.Entity.Interfaces.IAccount entity = (Sage.Entity.Interfaces.IAccount) this.BindingSource.Current;
ShowAllChanges(entity as Sage.SalesLogix.Orm.EntityBase);
ABOUT THE AUTHOR

Kris Halsrud

Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.

2 Comments

  1. HI, nice little code snippet. Are you sure it works on the OnAfterUpdate event? My understanding so far is, that the Changes get clered as soon as the save-method is executed. So I currently use it on OnBeforeUpdate. As you could also apply or revoce changes, this might not work on OnAfterUpdate.

    Reply
    • You are right Alexander, I meant the OnBeforeUpdate. I have updated the post.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) news and product updates!

You have Successfully Subscribed!