Q - I have a VF page, how can I check how many users have viewed my page and who has viewed last ?
Currently we don't have a standard functionality on Salesforce to get this. We can do this through Apex Controller and VF page.
We can create a field to save the number of time page accessed and a field to save the user who accessed the page last.
Create custom fields in Account -
Currently we don't have a standard functionality on Salesforce to get this. We can do this through Apex Controller and VF page.
We can create a field to save the number of time page accessed and a field to save the user who accessed the page last.
Create custom fields in Account -
- PageCount - Number Type
- Last VIewed By - Text Type
Create a Controller -
public class VFC_AccountPageViewCounter {
public Account acc {get;set;}
public User us {get;set;}
public decimal count;
public VFC_AccountPageViewCounter(ApexPages.StandardController controller){}
public pagereference updateCount() {
acc = [SELECT Id, bibhuotalightn__PageCount__c FROM Account Where Id =: ApexPages.currentPage().getParameters().get('Id')];
us = [SELECT name from User where Id=:Userinfo.getUserId()];
if(acc.bibhuotalightn__PageCount__c==null) {
acc.bibhuotalightn__PageCount__c = 1;
acc.bibhuotalightn__Last_Viewed_By__c = us.Name;
}
else {
acc.bibhuotalightn__PageCount__c += 1;
acc.bibhuotalightn__Last_Viewed_By__c = us.Name;
}
update acc;
return null;
}
}
Question - Why can't you write your logic inside Construcotr ? What is the need of a method again ?
Ans - As we can't write DML statement inside Constructor, we have to write a method for the logic to get the count and last viewed user details and can called the method on page load by using the page action attribute.

No comments:
Post a Comment