• Trevor0603
  • 0 Points
  • Member since 2009

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 2

This is my first week working with Salesforce, coming from a Coldfusion (don't be a hater!) and SQL Server background. 


Our users want to be able to see the number of Contacts for an account at a glance. To accommodate this, we've developed a custom field -- Account.Contact_Count__c which is an integer. Now for the hard part; populating it. We can't do a rollup because you can't roll up child data. So I wrote this bulk updater to do the work. I scheduled it (successfully) in the hopes that would get around the data governance limits, but no such luck.


We have thousands of accounts, and I won't know if they require updating unless I run a query on them. Is there a good way to run this in a background at low demand times? Is there a way that I can run it in blocks of data? Is there some super-duper easy way of running this that I simply didn't know about? Can I have this class call another class in order to reset the count of transactions?  Also, please note that as far as I can tell you can't combine count() with sub queries. I tried to make variations on this query work a number of different ways:


Select a.Contact_Count__C, count(Select ID From Contact Where AccountID = a.ID)

From Account a


I never got it to work. 


I'm currently orders of magnitude away from being able to execute this code. Help me understand that right way to accomplish a task like this in salesforce!


 I've run this both on-demand (so I can see errors) and scheduled. 

global class ModifyContractCount {

//completeRecount updates all records with the latest count of contacts

//the @future makes this available to be scheduled


public static void completeRecount()

{ //Identify the number of contacts for a particular account and update the Contact Count variable appropriately //Create list of records to update.

List<Account> AcctsToUpdate = [ SELECT A.ID, A.Contact_Count__c FROM Account A LIMIT 50 //NOTE: Works with 50 or fewer ];

//Loop through returned records

for(Account a : AcctsToUpdate)


//Identify number of child contacts

Integer NumberOfContacts =

[ SELECT count()FROM Contact WHERE AccountID = :a.Id


//If the Accounts.Contact_Count__c is not the same as the number of child contacts, update the record

if (NumberOfContacts != a.Contact_Count__c)


a.Contact_Count__c = NumberOfContacts; update a; } //end for loop } //end completeRecount method }


//If the Accounts.Contact_Count__c is not the same as the number of child contacts, update the record

if (NumberOfContacts != a.Contact_Count__c)

{a.Contact_Count__c = NumberOfContacts;

update a;}

//end for loop


//end completeRecount method }



Message Edited by BrendaFlynn on 03-05-2010 10:24 AM

I have a VF page, which is embedded (as a URL s-control) in the Contact page layout.

I just want to display some fields (the point is to keep these fields read only).

How do I pass make the VF page know which record's information to display (how do I pass this info, and how do I use it in the VF page side). thanks.