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
Geetha saiGeetha sai 

How to display the page view count in vf page

Hi,

    I have one custom object called AddsViewList.In that i have 2 custom fields i.e,AddsName,Count..in custom object i have 10 sample records.First req is i have to get the record name py passing id in vf page..Second one is i have to display the page view count whenever the user refresh the page and the count should be updated...

Can anyone pls provide the code..


 
Best Answer chosen by Geetha sai
Srinivas SSrinivas S
Hi Geetha,

Note: Please add a valid record id as parameter for the page as mentioned below -
https://c.ap2.visual.force.com/apex/PageViewCount?id=0012800000atO6J

Please find the following solution which is tested and working -
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>

-----------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.