Getting the Previous Value for a Field When Changed

When a field is changed in the 7.2 web client, you often will find scenarios where you will have business rules that need to validate the change and possibly change it back to whatever it was before. Checking the property on the entity, you’ll find it is too late. The property will already show the new value it was changed to. Fortunately, the 7.2 platform maintains state for all entities, so it is possible to easily retrieve the previous value for a property.

The following code will demonstrate how to gt the entity state for the account entity and retrieve the previous value for a property named MyProperty.

using System.Collections.Generic; //for IDictionary
using Sage.Platform.ComponentModel; //for ComponentChange
using Sage.Entity.Interfaces; //for IAccount
using Sage.SalesLogix.Orm; //for IEntityState
//...

IDictionary<string, ComponentChange> changes = (account as IEntityState).GetChanges();
if (changes.ContainsKey("MyProperty"))
{
    string oldValue = (string)changes["MyProperty"].OldValue;
    string newValue = account.MyProperty;

    // now do whatever you need to do with the oldValue and newValue
}

Keep in mind that the OldValue will be returned as an object, you’ll need to cast it into the proper type to use it. Enjoy!

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

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) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!