[SalesForce] How to get the username of salesforce user and count using Visual Force Page

I have created the Visualforce Page,I am giving the page access to all users such as System administrator user and internal users,I have created two new custom fields in account object such as Last Viewed user and count of the particular user

If sys administartor user view the page then the account object of the record,last Viewed user as sys administrator name and count will be 1,now another internal user viewing the page then last viewed user as internaluser name and now also count will be 1, because that particular user viewing the page now only,so his count will be 1

How to take the count Value for particular user and user name and how to store the value in Record of Account Object,Please Anyone Guide me for the Answer using Visual force and apex class

I have Tried:

Apex class:

public class DisplayPage {   
   public Decimal ViewCount {get; set;}


    public DisplayPage(ApexPages.StandardController Controller) {
        this.ViewCount = 0;
        TotalRecordUpdate();  
    }

    public void TotalRecordUpdate(){
        Id profileId = UserInfo.getProfileId();
        List<Page_View__c> pageViews = [SELECT Id, Count__c,AddsName_c__c FROM Page_View__c WHERE  AddsName_c__c = :profileId ];
       // system.debug(AddsName_c__c);
        system.debug(pageViews);
        system.debug(profileId);


        if(pageViews.size() == 0){
           Page_View__c pageView = new Page_View__c(Count__c = 1, AddsName_c__c = profileId);
           system.debug(pageView);
           this.ViewCount = 1;
        }else{
           pageViews[0].Count__c += 1;
           this.ViewCount = pageViews[0].Count__c;
           update pageViews[0];
        }
    }
}

Visual Force Page:

<apex:page standardController="Account" extensions="Displaypage" action="{!updateCount}">
<apex:form >
<apex:actionFunction name="updateCount" action="{!updateCount}" reRender="" />

<script>
    addEventListener("load", updateCount, false);
</script>
</apex:form>
</apex:page>

But it is Not working?
Please anyone Share the Logic and Answer

Best Answer

Problem

First of all, I don't think you need to use "Account" object or any other standard object for this requirement. You should go for the custom object, which in your case is, "Page_Views__c". And it should have at least these two fields:

  1. LastViewedBy (lookup to User)
  2. Count (Numeric, displaying the number of times this user has visited this page)

Logic

When a user is navigating to this VF page, you should check two things:

  1. If user record is not present in the pageview object, then INSERT record with user-lookup and count (1).
  2. If user record is present, then UPDATE with Count = +1

VF Page

<apex:page standardController="Page_Views__c" action="{!TotalRecordUpdate}" extensions="Displaypage" >
   <apex:form id="PageCount">
      <apex:actionFunction name="updateCount" action="{!updateCount}" reRender="PageCount" />
      Current user: {! $User.UserName } <br/>
      Page Count: {!ViewCount}
   </apex:form>
</apex:page>

Controller

public class DisplayPage {   
   public Decimal ViewCount {get; set;}

    public DisplayPage(ApexPages.StandardController Controller) {
        this.ViewCount = 0;
        //TotalRecordUpdate();  // no need to call dml or dml function in constructor
    }

    public void TotalRecordUpdate(){
        Id userid = UserInfo.getUserId();
        List<Page_View__c> pageViews = [SELECT Last_Viewed_By__c, Page_view_count__c FROM Page_View__c WHERE  Last_Viewed_By__c = : userid ];
   // system.debug(AddsName_c__c);
   if (pageViews.size() > 0 ){
       // record present, use Update
       ViewCount = pageViews[0].Page_view_Count__c +1;
       pageViews[0].Page_view_count__c = ViewCount;
       Update pageViews[0];
   }
        else{
            // record not present, Insert
            Page_View__c pv = new Page_View__c();
            pv.LastViewedBy = userid;
            ViewCount = 1;
            pv.Count__c = ViewCount;

            Insert pv;
            ViewCount = 1;
        }

    }
}

P.S. I have not tested the code above, just on-the-fly, so test it please.

Related Topic