We'd like methods on the generic SObject in Apex that lets us identify if a field has changed since it was retrieved from the database.
/**Returns true if any fields have been assigned to since the record was retrieved from the database*/
public Boolean hasChanged()
/**Returns true if the field has changed since it was returned from the database */
public Boolean hasChanged(String fieldName)
/**Returns the value the field had when it was returned from the database */
public Object previousValue(String fieldName)
A few use cases:
Sometimes additional validation rules (Apex validation rules) need to be applied if a value changes -- right now, this requires querying the object from the database a second time (governor limit cost, performance cost) and comparing the field values
For FLS enforcement, it is the Apex developer's responsibility to reject an Update/Upsert if the user doesn't have isUpdateable() on a field. This is huge burden for ISVs building managed packages. We had to implement: https://github.com/patronmanager/apex-dml-manager in order to hack around the lack of support for identifying dirty fields.
We're basically talking about the equivalent of Trigger.old and Trigger.new but within the context of a regular Apex execution context.
We appreciate that storing the previous value on the SObject could have VF view state and heap size implications so the implementation of previousValue() is a little messier -- a 'trackChanges()' method on SObject that could be called immediately after retrieval .. or perhaps an argument to Database.query to make the objects track changes