Work.com
Leading Through Change with Data
COVID-19 Data Hub
COVID-19 Global Daily Tracker
Global Economy Data Track
Government Data Track
Healthcare Data Track
B-Well Together
Leading Through Change
Salesforce Care
Leading Through Change Blog
Trailhead Resources
AppExchange Resources
MuleSoft Resources
< Back to List
David Esposito
Provide methods on SObject to identify dirty fields
Apex & Visualforce
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. Something like: class SObject { /**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
class SObject { /**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) }
· Flag
David Esposito - 4 years ago
getPopulatedFieldsAsMap() is not quite the same thing. It gives you the list of fields that were queried (or set) but not whether they had values changed during the duration of the transaction. We only want to enforce the FLS checks for fields that have changed since a user may rightfully be able to read a large number of fields on an SObject but only be able to write to a few of them
Tyler Mowbrey - 4 years ago
Chris Peterson - 6 years ago
Help us to keep IdeaExchange clean by pointing out overlapping ideas. We'll investigate your suggestion and merge the ideas if it makes sense.
Thanks for your merge suggestion. We will review it shortly and merge the ideas if applicable.
Salesforce takes abuse situations very seriously. Examples of abuse include but are not limited to posting of offensive language or fraudulent statements. To help us process your request as quickly as possible, please fill out the form below describing the situation. For privacy and security reasons, the final outcome of an abuse case may not be revealed to the person who reported it.
Thank you for your feedback. We take abuse seriously and will investigate this issue and take appropriate action.