+ Start a Discussion

How to track access by object?

We are trying to find ways to track adoption/usage of our application on force.com. Is there anyway to access frequency of access of custom objects by user?


Thanks in advance


Directly speaking, salesforce.com does not trigger any code when a record is viewed, so there is no way to track access using standard system features. You could, however, create a small visualforce page that tracks accesses. Consider the following:


Custom object definition:

Custom Object: Viewing History
  Name Field: Auto-Number
Custom Fields:
  User: Lookup(User)
  Date/Time Accessed: Date/Time
  Record Accessed: Lookup(Custom Object)

 Page (named whatever you like):

<apex:page standardController="Custom_Object__c" extensions="ViewingHistoryExtension" action="{!logAccess}"></apex:page>


public class ViewingHistoryExtension {
  Id viewedId;
  public ViewingHistoryExtension(apexpages.standardcontroller controller) {
    viewedid = controller.getrecordid();
  public void logaccess() {
    viewing_history__c h = new viewing_history__c(user__c=userinfo.getuserid(),date_time_accessed__c=system.now(),record_accessed__c=viewedId);
    insert h;

Then, simply add this page to each of your page layouts (you can use a height of zero so it appears invisible). Whenever a record is loaded, it will add a tracking record. You can then run reports on this information. Note that this will use a ton of database space, so you'll need to clear out this data periodically. You can track record updates by simply using a trigger to record this "viewing history" data.


A more advanced technique could also store this data as a JSON feed in the user's record as a long text area, but this reduces reporting capabilities (you'd have to actually write a custom visualforce page).