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
Sonu06Sonu06 

Hi guys, I am begineer in Salesforce developer, my question is I want to count the number of page refresh in visualforce page, so what will be

Hi guys, I am begineer in Salesforce developer, my question is I want to count the number of page refresh in visualforce page, so what will be  the vf code for this ??                                                                                                                
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sonal,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://developer.salesforce.com/forums/?id=906F0000000MN6KIAW

https://salesforce.stackexchange.com/questions/22912/how-to-count-the-number-of-times-a-visualforce-page-opened-by-user

https://www.forcetalks.com/salesforce-topic/visualforce-pages-count-number-of-times-a-page-is-loaded/

https://salesforce.stackexchange.com/questions/177325/count-the-number-of-times-a-visualforce-page-is-opened-using-apex-only/177326

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Sonal,
Please find the following solution :

--Helper Class:
public class SchemaGlobalDescribe{
    public static String findObjectNameFromRecordIdPrefix(String recordIdOrPrefix){
        String objectName = '';
        try{
            //Get prefix from record ID
            //This assumes that you have passed at least 3 characters
            String myIdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3);
             
            //Get schema information
            Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 
             
            //Loop through all the sObject types returned by Schema
            for(Schema.SObjectType stype : gd.values()){
                Schema.DescribeSObjectResult r = stype.getDescribe();
                String prefix = r.getKeyPrefix();
                System.debug('Prefix is ' + prefix);
                 
                //Check if the prefix matches with requested prefix
                if(prefix!=null && prefix.equals(myIdPrefix)){
                    objectName = r.getName();
                    System.debug('Object Name! ' + objectName);
                    break;
                }
            }
        }catch(Exception e){
            System.debug(e);
        }
        return objectName;
    }
}


--Controller Class:
public class PageViewCount {
    //assuming your custom object as Account
    Sobject sObj;
    public String recordName {get;set;}
    public Integer countVal {get;set;}
    public PageViewCount() {
        recordName = '';
        countVal = 0;
        Id recordId = (Id)ApexPages.currentPage().getParameters().get('Id');
        String objectName = SchemaGlobalDescribe.findObjectNameFromRecordIdPrefix(String.valueOf(recordId).substring(0,3));
        system.debug(recordId+'@@objectName: '+objectName);
        //sObj = (SObject)Type.forName(objectName).newInstance();
        sObj = Database.query('select Id, Name from '+objectName+' where Id=:recordId limit 1');       
        recordName = String.valueOf(sObj.get('Name'));
    }
    public void recordCount() {
        //assuming you are maintaining only single record in this object.
        List<AddsViewList__c> addsLst = [select Name, count__c from AddsViewList__c limit 1];
        if(addsLst == null || addsLst.isEmpty()) {
            AddsViewList__c addsView = new AddsViewList__c(count__c = 1);
            insert addsView;
            countVal = Integer.valueOf(addsView.count__c);
        }
        else {
            if(addsLst[0].count__c == null)
                addsLst[0].count__c = 1;
            else 
                addsLst[0].count__c++;
            countVal = Integer.valueOf(addsLst[0].count__c);
            upsert addsLst;
        }
    }
}



--Visualforce Page:
<apex:page controller="PageViewCount" action="{!recordCount}">
    <apex:form>
        <apex:pageBlock title="Number of Page Views">
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Name:"/>
                    <apex:outputText value="{!recordName}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Count:"/>
                    <apex:outputText value="{!countVal}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>




I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
 
vikrant Chauhan 5vikrant Chauhan 5
Hi Ajay Dubedi,
This code is not working .There are lot of errors are coming.