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
BerettaJonBerettaJon 

Trying to update a record with visual force and apex

I have an empty visual force page on my Account records.  This visualforce page has 

extensions="AccountRollUpController" action="{!getCounts}">

and the apex that it is calling looks like 

public void getCounts()
    {
        this.account.of_Contacts__c = getintNumberOfContacts();
        this.account.of_Open_Activities__c = getintNumberOfOpenActivities();
        this.account.of_Closed_Activities__c = getintNumberOfClosedActivities();
        update this.account;   
    }



The problem is that this code seems to be run after the Account Record is loaded. For instance, the very first time I view and account record the 3 fields above are blank, if I refresh the page the correct values appear. I also have a problem when, from the account record, a user chooses to add a contact, when they are returned to the account record the count of contacts isnt correct until the account record is refreshed.

Help? :)



Best Answer chosen by Admin (Salesforce Developers) 
BerettaJonBerettaJon

Here is the solution I came up with.  I accepted a comment in the Apex board, but here is exactly how I ended up doing it for anyone else looking in the future. :)

 

Comments, critiques, and criticisms are welcome!

 

public with sharing class AccountRollUpController {

    public Account account{get;set;}
    public ApexPages.StandardController stdController;
    Integer intNumberOfContacts;
    Integer intNumberOfOpenActivities;
    Integer intNumberOfClosedActivities;
    
    public AccountRollUpController(ApexPages.StandardController controller) {
        stdController = controller;
        this.account = (Account)controller.getRecord();
    }   
    public PageReference getCounts()
    {
          this.account = (Account)stdController.getRecord();          
          account = [SELECT of_Contacts__c, of_Open_Activities__c, of_Closed_Activities__c FROM Account WHERE ID = :account.ID];
                   
        if((this.account.of_Contacts__c != getintNumberOfContacts()) || (this.account.of_Open_Activities__c != getintNumberOfOpenActivities()) || (this.account.of_Closed_Activities__c != getintNumberOfClosedActivities()))
        {
            System.debug('$$$$');
            this.account.of_Contacts__c = getintNumberOfContacts();
            this.account.of_Open_Activities__c = getintNumberOfOpenActivities();
            this.account.of_Closed_Activities__c = getintNumberOfClosedActivities();
            update this.account;
            return new PageReference('/' + this.account.Id);
        }
        else
        return null;
    }
    public Integer getintNumberOfContacts()
    {
        intNumberOfContacts = [SELECT count() FROM Contact WHERE AccountID = :account.ID];  
        return intNumberOfContacts;
    }
    public Integer getintNumberOfOpenActivities()
    {
        intNumberOfOpenActivities = [SELECT count() FROM Task WHERE AccountID = :account.ID and status != 'Completed'];
          
        return intNumberOfOpenActivities;
    }
    public Integer getintNumberOfClosedActivities()
    {
        intNumberOfClosedActivities = [SELECT count() FROM Task WHERE AccountID = :account.ID and status = 'Completed'];  
        return intNumberOfClosedActivities;
    }
    
}