How can I get the last modified by, and last modified date for a custom field on a custom object in apex. I need to display this information on front-end.
-
Is this for admin use, or general user use?– sfdcfoxApr 22, 2018 at 1:21
-
General use.. need to show on ui under the fields which are editable– AshuApr 22, 2018 at 2:17
-
So, you want the last time the field's definition was updated? It sounds more like you want to know the last time a particular value was updated?– sfdcfoxApr 22, 2018 at 3:00
-
@sfdcfox that's correct. I want to know the last time a particular value was updated and who updated it.– AshuApr 22, 2018 at 3:03
3 Answers
For that, you need to have Field History Tracking enabled. The general Last Modified Date audit field only shows the last time the record was updated, not individual values on the record. If you don't enable tracking, or don't log the changes yourself, you have no way to discern when the values were last updated. Please note that you can only track 20 fields, and the tracking is not retroactive, and has a limited life time (is eventually deleted). Once you've enabled it, Salesforce logs an entry in the appropriate CustomObject__History table, that shows the modification time, the old value, the new value, and who did it. To actually display the values beneath the field, you need Visualforce/Lightning Components that can query the History table and find the most recent entry for the field.
-
Hey, thanks so much for the detailed answer, it is really informative. The field history tracking is already enabled but still the query that I am using is not returning anything. Can you please help with some query that might work.– AshuApr 22, 2018 at 3:11
-
@Ashu Again, the table is trimmed periodically. If there were no edits since tracking was enabled, there also won't be any results. Also, history tracking does not work in unit tests, as far as I'm aware.– sfdcfoxApr 22, 2018 at 3:13
You can try using the query below to query for the last modified by and last modified date in your apex controller, then display them to your front-end.
Select Id, LastModifiedBy.Name, LastModifiedDate From Custom_Object__c
-
1
-
-
I would suggest that you enable field history tracking for your custom object. Then run the query: SELECT ParentId, OldValue, NewValue, Field, CreatedBy.Name, CreatedDate FROM CustomObject__History where Field = 'CustomField__c'. Let me know if that works. Apr 22, 2018 at 3:04
-
Hey.. thanks.. I had been trying something similar too but it doesn't return anything. I tried the query that you mentioned and it is not returning anything.– AshuApr 22, 2018 at 3:08
Currently Field History tracking will be the Native Salesforce Solution as mentioned here before - but perhaps this is somehow limited for this requirement- if needed to track specific values change that happens on your custom VF page or Lightning comp. Maybe you should consider implementing a simple JS function on client side to have a time stamp on-change event for each tracked value and then store it on your own custom object?