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
vijay kumar kvijay kumar k 

count no of contacts under account using batch class

write batch class to update field(noofcontacts__c) value on account with No of Contacts under that account.
 
Best Answer chosen by vijay kumar k
NagendraNagendra (Salesforce Developers) 
Hi Vijay,

Here is a simple way to wrap this up in a Batchable. All the Batchable mechanism is being used for is to divide up the Accounts into blocks.
public class MyBatchable implements Database.Batchable<SObject> { public Database.QueryLocator start(Database.BatchableContext context) { return Database.getQueryLocator([select Id from Account order by Name]); } public void execute(Database.BatchableContext context, List<Account> scope) { Account[] updates = new Account[] {}; for (AggregateResult ar : [ select AccountId a, count(Id) c from Contact where AccountId in :scope group by AccountId ]) { updates.add(new Account( Id = (Id) ar.get('a'), No_of_Contacts__c = (Decimal) ar.get('c') )); } update updates; } public void finish(Database.BatchableContext context) { } }
If you have only a small number of Contacts per Account you can run this with largish batch size, say the default of 200. But if you have more than 50,000 Contacts related to any Account this will still fail with even a batch size of 1 because the governor limits apply to each execute method call.

To run the Batchable it's:

Database.executeBatch(new MyBatchable(), 50);

where the last argument is how many Account object to pass per call of the Batchable's execute method.

Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Vijay,

Here is a simple way to wrap this up in a Batchable. All the Batchable mechanism is being used for is to divide up the Accounts into blocks.
public class MyBatchable implements Database.Batchable<SObject> { public Database.QueryLocator start(Database.BatchableContext context) { return Database.getQueryLocator([select Id from Account order by Name]); } public void execute(Database.BatchableContext context, List<Account> scope) { Account[] updates = new Account[] {}; for (AggregateResult ar : [ select AccountId a, count(Id) c from Contact where AccountId in :scope group by AccountId ]) { updates.add(new Account( Id = (Id) ar.get('a'), No_of_Contacts__c = (Decimal) ar.get('c') )); } update updates; } public void finish(Database.BatchableContext context) { } }
If you have only a small number of Contacts per Account you can run this with largish batch size, say the default of 200. But if you have more than 50,000 Contacts related to any Account this will still fail with even a batch size of 1 because the governor limits apply to each execute method call.

To run the Batchable it's:

Database.executeBatch(new MyBatchable(), 50);

where the last argument is how many Account object to pass per call of the Batchable's execute method.

Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Vijay,

If you want to get the no of contacts in account by using batch class.

Please have a look at this link:
https://salesforce.stackexchange.com/questions/81765/batch-apex-for-counting-number-of-contacts-associated-with-account

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Soufiyane IbrizSoufiyane Ibriz
User-added image
Can you please help me to solve it