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) 
Alex.AcostaAlex.Acosta

Otherwise all I can really say is just redirect your page back to itself and stop it from being in a continous loop by passing in parameters. ie:

 

private Map<String, String> pageParams;

public AccountRollUpController(ApexPages.StandardController stdController) {
    pageParams = ApexPages.currentPage().getParameters();
    ....
}

public PageReference getCounts(){
   if(pageParams.contains('refreshPage') && pageParams.get('refreshPage') == 'false')
return null;
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('/apex/<VisualforcePageName>?id=' + this.account.Id + '&refreshPage=false'); }

 

All Answers

Alex.AcostaAlex.Acosta
actionApexPages.ActionThe action method invoked when this page is requested by the server. Use expression language to reference an action method. For example, action="{!doAction}" references the doAction() method in the controller. If an action is not specified, the page loads as usual. If the action method returns null, the page simply refreshes. This method will be called before the page is rendered and allows you to optionally redirect the user to another page. This action should not be used for initialization.

 

Try and see if you can change your action method into a PageReference and return null and see if your information is refreshed before the load of the page. Otherwise, I suggest just using your controller and repull back your record as you have updated it.

 

 

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

return null; }

 

Otherwise you can try this....

   public AccountRollUpController(ApexPages.StandardController stdController) {
        ....
    }

    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;   

        this.account = (Account)stdController.getRecord();
    }
BerettaJonBerettaJon

Returning a null pagereference did not change the behavior. 

 

I dont understand the second solution as the stdContoller is out of scope and does not compile.

 

this.account = (Account)stdController.getRecord();

Would this refresh the record/page I am on if the stdController object was in scope?

Alex.AcostaAlex.Acosta

The second example was suppose to show your extension class. I don't know what your Standard Controller name was so I was just trying to short hand give you an example. Verify what you have as the parameter name on your constructor which is your controller class matches what I have to repull back the account record.

Alex.AcostaAlex.Acosta

Otherwise all I can really say is just redirect your page back to itself and stop it from being in a continous loop by passing in parameters. ie:

 

private Map<String, String> pageParams;

public AccountRollUpController(ApexPages.StandardController stdController) {
    pageParams = ApexPages.currentPage().getParameters();
    ....
}

public PageReference getCounts(){
   if(pageParams.contains('refreshPage') && pageParams.get('refreshPage') == 'false')
return null;
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('/apex/<VisualforcePageName>?id=' + this.account.Id + '&refreshPage=false'); }

 

This was selected as the best answer
BerettaJonBerettaJon

Sorry we are not understanding each other.  Here is my constructor.

 

public AccountRollUpController(ApexPages.StandardController controller) {

}
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;
this.account = (Account)controller.getRecord();

}

 

When I try to save that it says Compile Error: Method does not exist or incorrect signature: controller.getRecord() at line 24 column 33

BerettaJonBerettaJon

I will try the PageReference method of redirecting to the same account record.

 

EDIT

 

Is this the way to redirect to an Account record as well as VisualForce pages?

return new PageReference('/apex/<VisualforcePageName>?id=' + this.account.Id + '&refreshPage=false');
Alex.AcostaAlex.Acosta

Yes, though if you are not getting it to refresh properly, you may want to do this...

 

PageReference pg = new PageReference('/apex/<VisualforcePageName>?id=' + this.account.Id + '&refreshPage=false');
pg.setRedirect(true);
return pg;

 

As far as the issue with your controller, sorry for not explaining it correctly... your constructor method only has your Controller class locally, you'll have to make a global variable in order to access your controller instance anywhere within your class, otherwise it's only accessible within your constructor.

 

ApexPages.StandardController myController;
 
public AccountRollUpController(ApexPages.StandardController controller) {
    myController = controller;
 
    ....
}
   
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;
   
    this.account = (Account)myController.getRecord();
 }
BerettaJonBerettaJon

Here is the solution I came up with.  I accepted a comment above since it was helpful 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;
    }
    
}