I have created the Visualforce Page ,if that page is Viewed by Particular user,their count and name should be recorded in the record of custom object,for every user count is different,is it possible to create the picklist value should be dependent on user?

For example sys admin user Viewing page, the count should be displayed as 1 and portal user Viewing the page the count should be displayed as 1 and next time sys admin user viewing the page the count should be 2,like that so on

Apex class:

    public class DisplayPage {
    public string CurrentRecordID {get;set;}
    public  list<Page_View__c> viewList {get;set;}
    public static string CurrentRecordID1 {get;set;}

   public DisplayPage(ApexPages.StandardController Controller) {
    public static void TotalRecordUpdate() {

        List <  Page_View__c > totalupdate = new List < Page_View__c > ();
        List <  Page_View__c > recDetails = [select id, Count__c from   Page_View__c where id ='a0L28000003YzNx'];
        for (Page_View__c updateCount: recDetails)
          updateCount.Count__c = updateCount.Count__c++ == NULL ? 1 : updateCount.Count__c++;
           updateCount.AddsName_c__c= userinfo.getusername();

        update totalUpdate;

Vf Page:

<apex:page sidebar="false" showheader="false" StandardController="Page_View__c" extensions="DisplayPage" action="{!TotalRecordUpdate}">
    <apex:pageBlock >
        <apex:form />
        <apex:pageBlockSection >

1 Answer 1


You can determine the Profile of the current user using the {!$Profile.Id} and {!$Profile.Name} in your Visualforce page.

Regarding the User information, you can use the {!User.Id} for example. For more info about the available User fields follow the link to official documentation: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_global_user.htm

The above two global variables are usable within your Visualforce page. If you need to access user information within your controller (which I think you do), you can use UserInfo class, specificaly the UserInfo.getProfileId() and UserInfo.getUserId().

I think that with these tools you can obtain the required information to pretty easily implement you requirement.


If I understood you correctly, you need to update the record for specific profile only, and not update the same Page_View__c record all the time. In your current code you are basically keeping a count of ALL of your page views, independent of the profile. Your code should be like this:


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

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

    public void updateViewCount(){
        Id profileId = UserInfo.getProfileId();
        List<Page_View__c> pageViews = [SELECT Id, Count__c FROM Page_View__c WHERE Profile_Id__c = :profileId ];

        if(pageViews.size() == 0){
           Page_View__c pageView = new Page_View__c(Count__c = 1, Profile_Id__c = profileId);
           insert pageView;
           this.ViewCount = 1;
           pageViews[0].Count__c += 1;
           update pageViews[0];
           this.ViewCount = pageViews[0].Count__c;            

VF Page:

<apex:page sidebar="false" showheader="false" StandardController="Page_View__c" extensions="DisplayPage">
    Total view count for profile {!$Profile.Name} is {!ViewCount}

I haven't compiled this code anywhere, I just wrote it here, but I believe it should work. Let me know!

  • I am getting the username like userinfo.getusername();depending the user name another picklist field called count is controllable,for example sys admin user the count as 2,portal user as 1,is it possible
    – user30475
    Commented Sep 6, 2016 at 6:15
  • for particular organization there are 10 users are there?the code should have to work
    – user30475
    Commented Sep 6, 2016 at 6:16
  • I am sending my code both vf page and controller class,please alter the code for my reqiremant@smukov
    – user30475
    Commented Sep 6, 2016 at 6:17
  • What is exactly the problem with your code? Can you add it to your original question?
    – smukov
    Commented Sep 6, 2016 at 6:22
  • Depending on the user the count field should be displayed How?Thats all @Smukov,How to filter the Count fields Depending on the user
    – user30475
    Commented Sep 6, 2016 at 6:25

You must log in to answer this question.