You need to sign in to do that
Don't have an account?
Julius 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>
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>
<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
May I suggest you please refer the below link for reference to keep a track of who viewed a record.
- https://success.salesforce.com/answers?id=9063000000158mCAAQ
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
<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>
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;
}
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;
}