1

I have Created the Visualforce Page and apex class which Means Controller Class to count the Viewers of users using the Visualforce Page

VisualForce Page:

<apex:page sidebar="false" showheader="false" StandardController="Page_View__c" extensions="DisplayPage" action="{!TotalRecordUpdate}">
    <apex:pageBlock >
        <apex:form />
        <apex:pageBlockSection >
            title="your record details of ID:{!CurrentRecordID}"
        </apex:pageBlockSection>
    </apex:pageBlock>

    <apex:outputField value="{!Page_View__c.Count__c}" />
</apex:page>

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) {
        CurrentRecordID = ApexPages.currentPage().getparameters().get('id');
        system.debug(CurrentRecordID);
        viewList = [select id, Count__c from    Page_View__c where id =: CurrentRecordID];
        system.debug(viewList);
        DisplayPage.TotalRecordUpdate();
    }
    public static void TotalRecordUpdate() {
        CurrentRecordID1 = ApexPages.currentPage().getparameters().get('id');
        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 =: CurrentRecordID1];
        for (   Page_View__c updateCount: recDetails)
        {
            updateCount.Count__c = updateCount.Count__c++;
            totalUpdate.add(updateCount);
        }
        update totalUpdate;
    }
}

I am getting the Debug Log of currentRecordId is Null,How to Rectify the Error,How to get the Id Value in CurrentRecordid Anyone Guide Me for the Answer

1 Answer 1

0

What's the URL with which you are opening your Visualforce page? Does it have the Id parameter?

You need to add the Id to your URL, so that URL looks similar to this: /apex/MyVFPage?Id=a4C90000000PIax

Otherwise, the ApexPages.currentPage().getparameters().get('id') will return NULL

UPDATE

You can't call the ApexPages.currentPage().getparameters().get('id') inside the static method. Try passing the Id as a parameter to your TotalRecordUpdate function when calling it from constructor. You already have the Id in CurrentRecordID variable.

6
  • it is showing error like this:System.NullPointerException: Attempt to de-reference a null object Class.DisplayPage.TotalRecordUpdate: line 18, column 1 Class.DisplayPage.<init>: line 10, column 1
    – SFDC
    Commented Sep 1, 2016 at 9:51
  • How to Rectify the Error
    – SFDC
    Commented Sep 1, 2016 at 9:52
  • @user36188 see my update
    – smukov
    Commented Sep 1, 2016 at 10:07
  • Now what i have to do @smukov
    – SFDC
    Commented Sep 1, 2016 at 10:09
  • if i use Direct variable , it is not showing CurrentRecordId is Not exist what i have to do
    – SFDC
    Commented Sep 1, 2016 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .