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
SpunkySpunky 

Increment counter, save value and rerender on page

I used this code as is (borrowed from visualforce documentation)  on my sites page.

It's labeled "update" and everytime it clicks, the value increments

However when the sites page is refreshed, it resets to zero.

 

What I need is to save the value everytime it increments to a field called "#ofUpdates" because I'd like to display this value alongside the counter link on the sites page. 

 

How do I go about modifying the code to achieve this? Any help would be greatly appreciated.

 

<apex:page controller="exampleCon">
    <apex:form >
        <apex:outputText value="Update: {!count}" id="counter"/>
        <apex:actionStatus id="counterStatus">
            <apex:facet name="start">
                 <img src="{!$Resource.spin}"/> <!-- A previously defined image --> 
   
            </apex:facet>
        </apex:actionStatus>   
        <apex:actionPoller action="{!incrementCounter}" rerender="counter"
            status="counterStatus" interval="7"/>
    </apex:form>
</apex:page>

 

Controller

public class exampleCon {
    Integer count = 0;
                       
    public PageReference incrementCounter() {
            count++;
            return null;
    }
                       
    public Integer getCount() {
        return count;
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

If you want to save the counter so that if you start next time it starts from last count, you have to save count every time it gets increased , I saved this in Account Objects NumberOfEmplyee field you can use any other field or custom setting.

 

https://c.ap1.visual.force.com/apex/IncrementCounterPage

 

Apex Class

public without sharing class exampleCon {
    Integer count = 0;
    public Account a {get;set;}        
    
    public exampleCon()
    {
        Account a = new Account();
        a = [Select NumberOfEmployees from Account where id = '00190000007RBhM'];
        if(a.NumberOfEmployees != null)
            count = a.NumberOfEmployees;
    }           
    public PageReference incrementCounter() {
            count++;
            try
            {
                a = [Select id from Account where id = '00190000007RBhM'];
                a.NumberOfEmployees = count;
                update a;
            }
            catch(exception e)
            {
                ApexPages.addMessages(e);
            }
            return null;
    }
       
                       
    public Integer getCount() {
        return count;
    }
}

 VFP

<apex:page controller="exampleCon">
    <apex:form >
        <apex:pageMessages id="pgMsg"></apex:pageMessages>
        
        <apex:outputText value="Update: {!count}" id="counter"/>
        <apex:actionStatus id="counterStatus">
            <apex:facet name="start">
                 <apex:outputLabel value="In Creament in Progress"></apex:outputLabel>
    
            </apex:facet>
        </apex:actionStatus>    
        <apex:actionPoller action="{!incrementCounter}" rerender="counter,pgMsg"
            status="counterStatus" interval="7"/>
    </apex:form>
</apex:page>

 You can see this on here on my site page

https://c.ap1.visual.force.com/apex/IncrementCounterPage

You can see this on here on my site page

All Answers

Shashikant SharmaShashikant Sharma

Here it is on my site page

http://metacubepoc-developer-edition.ap1.force.com/IncrementCounterPage

 

Working fine

If your issue is that when you refresh the page by hitting URL again then it will rest to zero as no view state will be there then.

 

Here is my code

Apex Class:

public class exampleCon {
    Integer count = 0;
                        
    public PageReference incrementCounter() {
            count++;
            return null;
    }
                        
    public PageReference currentCount()
    {
        return ApexPages.currentpage();
    }  
                       
    public Integer getCount() {
        return count;
    }
}

 VFP

 

<apex:page controller="exampleCon">
    <apex:form >
        <apex:commandButton value="Show Current Count" action="{!currentCount}"/>
        <apex:outputLabel value="Current Count : {!count}"></apex:outputLabel>
        
        <br/>
        <br/>
        
        <apex:outputText value="Update: {!count}" id="counter"/>
        <apex:actionStatus id="counterStatus">
            <apex:facet name="start">
                 <apex:outputLabel value="In Creament in Progress"></apex:outputLabel>
    
            </apex:facet>
        </apex:actionStatus>    
        <apex:actionPoller action="{!incrementCounter}" rerender="counter"
            status="counterStatus" interval="7"/>
    </apex:form>
</apex:page>

 Please let me know if I could not get you issue.

Shashikant SharmaShashikant Sharma

If you want to save the counter so that if you start next time it starts from last count, you have to save count every time it gets increased , I saved this in Account Objects NumberOfEmplyee field you can use any other field or custom setting.

 

https://c.ap1.visual.force.com/apex/IncrementCounterPage

 

Apex Class

public without sharing class exampleCon {
    Integer count = 0;
    public Account a {get;set;}        
    
    public exampleCon()
    {
        Account a = new Account();
        a = [Select NumberOfEmployees from Account where id = '00190000007RBhM'];
        if(a.NumberOfEmployees != null)
            count = a.NumberOfEmployees;
    }           
    public PageReference incrementCounter() {
            count++;
            try
            {
                a = [Select id from Account where id = '00190000007RBhM'];
                a.NumberOfEmployees = count;
                update a;
            }
            catch(exception e)
            {
                ApexPages.addMessages(e);
            }
            return null;
    }
       
                       
    public Integer getCount() {
        return count;
    }
}

 VFP

<apex:page controller="exampleCon">
    <apex:form >
        <apex:pageMessages id="pgMsg"></apex:pageMessages>
        
        <apex:outputText value="Update: {!count}" id="counter"/>
        <apex:actionStatus id="counterStatus">
            <apex:facet name="start">
                 <apex:outputLabel value="In Creament in Progress"></apex:outputLabel>
    
            </apex:facet>
        </apex:actionStatus>    
        <apex:actionPoller action="{!incrementCounter}" rerender="counter,pgMsg"
            status="counterStatus" interval="7"/>
    </apex:form>
</apex:page>

 You can see this on here on my site page

https://c.ap1.visual.force.com/apex/IncrementCounterPage

You can see this on here on my site page

This was selected as the best answer
SpunkySpunky

Thanks so much. This is exactly what i needed.