function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Julius SalvadorJulius Salvador 

Track who last viewed a record

Hello Guru's,

I've created an Apex Class, a Visual Force Page, and a custom field named "Last Viewed By" earlier, the goal is to have the feature of knowing who last viewed an account record in SF. Both of the Apex Class and Visual Force page are working fine, although there's a bit issue, It messes up the "Last Modified By" Field as well. So for example: USER 1 viewed the Account named Account 1 (viewed only, no editting done), when User 2 views the same account both of the "Last Viewed By" and "Last Modified By" field are now showing USER 1 DD/MM/YYYY TIME. Any advise, or suggestions on what changes needs to be done on the codes below.

APEX CLASS
public class lastViewedAccount{
public Datetime cDT;
public String LongDate;
public String firstname;
public String lastname;
public String userid;
private final Account acct;
public lastViewedAccount(ApexPages.StandardController stdController) {
this.acct = (Account)stdController.getRecord(); }
public String getLongDate() {
cDT = System.now(); //Format the datetime value to your locale
LongDate = cDT.format('dd/MM/yyyy HH:mm'); return LongDate;
}
public void updateField() {
//Get the user info from the current user
firstname = System.Userinfo.getFirstName();
lastname = System.Userinfo.getLastName();
userid = System.Userinfo.getUserId();
//Get the Account record to be updated
Account a = [select Last_Viewed_By__c from Account where id = :acct.id limit 1];
//Assign values to Last Viewed By field & update the record
a.Last_Viewed_By__c = (firstname + ' ' + lastname + ', ' + getLongDate());
update a;
}

}

Visual Force Page
<apex:page action="{!updateField}" extensions="lastViewedAccount" showheader="false" sidebar="false" standardcontroller="Account"> </apex:page>
Best Answer chosen by Julius Salvador
Glyn Anderson 3Glyn Anderson 3
Julius,  The problem is that if you update the Account record to modify the Last_Viewed_By__c field, then you are modifying the record - and the LastModifiedBy field is also updated.  You could create a custom object, Account_View_History__c, that has a master-detail to Account, and a lookup to User (and maybe a DateTime field).  Create no more than one per Account, and update that record each time the Account is viewed.  Since you never update the Account record itself, it won't affect the LastModifiedBy value.  You can see who last viewed the record in the related list of Account_View_History__c records (add the User field to the related list layout).

<pre>
public void updateField()
{
    Account_View_History__c avh = new Account_View_History__c();
    for ( Account_View_History__c oldAvh :
        [   SELECT  Id, Account__c, User__c, DateTime__c
            FROM    Account_View_History__c
            WHERE   Account__c = :acct.Id
            LIMIT 1
        ]
        )
    {
        avh = oldAvh;
    }
    avh.Account__c = acct.Id;
    avh.User__c = UserInfo.getUserId();
    avh.DateTime__c = DateTime.now();
    upsert avh;
}
</pre>
 

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Julius Salvador,

May I suggest you please refer the below link for reference to keep a track of who viewed a record. Hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar

 
Glyn Anderson 3Glyn Anderson 3
Julius,  The problem is that if you update the Account record to modify the Last_Viewed_By__c field, then you are modifying the record - and the LastModifiedBy field is also updated.  You could create a custom object, Account_View_History__c, that has a master-detail to Account, and a lookup to User (and maybe a DateTime field).  Create no more than one per Account, and update that record each time the Account is viewed.  Since you never update the Account record itself, it won't affect the LastModifiedBy value.  You can see who last viewed the record in the related list of Account_View_History__c records (add the User field to the related list layout).

<pre>
public void updateField()
{
    Account_View_History__c avh = new Account_View_History__c();
    for ( Account_View_History__c oldAvh :
        [   SELECT  Id, Account__c, User__c, DateTime__c
            FROM    Account_View_History__c
            WHERE   Account__c = :acct.Id
            LIMIT 1
        ]
        )
    {
        avh = oldAvh;
    }
    avh.Account__c = acct.Id;
    avh.User__c = UserInfo.getUserId();
    avh.DateTime__c = DateTime.now();
    upsert avh;
}
</pre>
 
This was selected as the best answer
Julius SalvadorJulius Salvador
@Glyn tried your suggestion mate however I cannot get pass this error, any suggestion?

Error: Compile Error: unexpected token: 'Void' at line 1 column 7

Public Void updateField()
{
Account_View_History__c avh = new Account_View_History__c();
for ( Account_View_History__c oldAvh :
[   SELECT  Id, Account__c, User__c, Last_Viewed_Date__c
FROM    Account_View_History__c
WHERE   Account__c = :acct.Id
LIMIT 1
 ]
)
{
avh = oldavh;
}
avh.Account__c = acct.Id;
avh.User__c = UserInfo.getUserId();
avh.Last_Viewed_Date__c = DateTime.now();
upsert avh;
}
Glyn Anderson 3Glyn Anderson 3
Julius,  An error like that is usually by something that come before.  Can you post the entire class, please?
Julius SalvadorJulius Salvador
Tried using this one mate.

Public Void updateField()
{
Account_View_History__c avh = new Account_View_History__c();
for ( Account_View_History__c oldAvh :
[   SELECT  Id, Account__c, User__c, Last_Viewed_Date__c
FROM    Account_View_History__c
WHERE   Account__c = :acct.Id
LIMIT 1
 ]
)
{
avh = oldavh;
}
avh.Account__c = acct.Id;
avh.User__c = UserInfo.getUserId();
avh.Last_Viewed_Date__c = DateTime.now();
upsert avh;
}
Glyn Anderson 3Glyn Anderson 3
OK, but that's just the updateField method.  I need to see what precedes it in the class.  Can you post the enitre class?
Glyn Anderson 3Glyn Anderson 3
Ahh, I see.  The method starts on line 1, which means it's not in a class.  I meant for you to replace the updateField method that you had in your lastViewedAccount class (from your original post).  Everything else in the class can stay as it was.
Julius SalvadorJulius Salvador
Thanks Glyn! Helped alot mate.