You need to sign in to do that
Don't have an account?

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? :)
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:
All Answers
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.
Otherwise you can try this....
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.
Would this refresh the record/page I am on if the stdController object was in scope?
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.
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:
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
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?
Yes, though if you are not getting it to refresh properly, you may want to do this...
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.
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;
}
}