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;

    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);

        if(pageViews.size() == 0){
           Page_View__c pageView = new Page_View__c(Count__c = 1, AddsName_c__c = profileId);
           this.ViewCount = 1;
           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="" />

    addEventListener("load", updateCount, false);

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

  • you can create separate object with all the accountId, user name, number of hits etc. attributes and store those values – Santanu Boral Nov 8 '16 at 6:44
  • Can you tell us more about what are you trying to achieve ... if you are thinking about locking the record based on number of users accessing the record, there are few issues with this way called pessimistic record locking – Amr Ibrahim Nov 8 '16 at 6:46
  • @AmrIbrahim If the user view the the visualforce page ,his user name should be stored in the account old record of salesforce in the Custom field called Last Viewed user and how many times that particular user view the page should be stored in count field – SFDC Nov 8 '16 at 6:49


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)


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}


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];
            // 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.

| improve this answer | |
  • ok i will test it and i will tell what is my problem – SFDC Nov 8 '16 at 12:11
  • while clicking the preview in the Visual force page it is Showing like that DML currently not allowed An unexpected error has occurred. Your development organization has been notified. – SFDC Nov 8 '16 at 12:44
  • I updated the code and it should not display the error that you were having. But I have tested the code myself and @LanceShi was right. It is giving error "DML currently not allowed". I shall look into his suggestion and see any other workaround. In the meantime, you too, check for other solutions. – Mahmood Nov 8 '16 at 12:45
  • ok Mahmood Meanwhile i will check for alternate Solution let me know if you find any alternate answer,Thank you for spending time with me – SFDC Nov 8 '16 at 12:46
  • 1
    I have updated the code, and used "Action" attribute on the apex page level to call the method and removed the method call from constructor. It is working and I have tested it but it might not be the best practice. I haven't worked on JS remoting but will explore it. I suggest you do the same. – Mahmood Nov 8 '16 at 13:36

I don't really think you can do a DML in your page action or controller's constructor. Nor should you do it. Alternatively, you can make a Javascript remoting call to your controller's static method. And do the DML in your static method. Hope this make sense.

| improve this answer | |
  • I am not getting your point ,how to store the count Value for every user, for eg: system admin user count will be 3,,sys admin user view the page three times only and internal user count will be 2, can you please share the code for my criteria @Lanceshi – SFDC Nov 8 '16 at 7:04
  • Here actually mentioned as pageView object instead of account Object – SFDC Nov 8 '16 at 7:07
  • guide me the code of apex class and Visual force page – SFDC Nov 8 '16 at 7:13
  • please provide me the code @Lanceshi,I have tried from my side so many logics I am not able to store the count field value for seperate user ,Guide me for the Answer – SFDC Nov 8 '16 at 7:25
  • why we have to use Remote action in apex class? what is the use? @Lance shi – SFDC Nov 8 '16 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.