Activity › Forums › Salesforce® Discussions › Returning no. of records processed by a batch class in Salesforce
Tagged: Batch Class, Multiple Records, Salesforce Apex, Salesforce Customization, Salesforce Development, Salesforce Records, Salesforce SOQL
-
Returning no. of records processed by a batch class in Salesforce
Posted by Kumar on December 15, 2016 at 6:18 AMHi everyone,
I am using a batch class to perform some DML on a list of accounts. I want to return the no. of records proccessed by this batch class, i.e. how many records were successfully processed and how many were not.
How do I implement this?
Thanks
Shekhar Gadewar replied 9 years, 5 months ago 2 Members · 6 Replies -
6 Replies
-
Have you tried using database.stateful in that class?
Do let me know if any help needed in it.
Regards,
Shekhar
-
This reply was modified 9 years, 5 months ago by
Shekhar Gadewar.
-
This reply was modified 9 years, 5 months ago by
- [adinserter block='9']
-
Great!
Please share the code snippet with the group so it will help others too.
-
Sure, this is the code snippet:
global class UpdateContactAddresses implements
Database.Batchable<sObject>, Database.Stateful {global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(‘SELECT ID, BillingStreet, BillingCity, BillingState,BillingPostalCode, (SELECT ID, MailingStreet, MailingCity,MailingState,MailingPostalCode FROM Contacts) FROM Account Where BillingCountry = \’USA\”);
}global void execute(Database.BatchableContext bc, List<Account> scope){
List<Contact> contacts = new List<Contact>();
for (Account account : scope) {
for (Contact contact : account.contacts) {
contact.MailingStreet = account.BillingStreet;
contact.MailingCity = account.BillingCity;
contact.MailingState = account.BillingState;
contact.MailingPostalCode = account.BillingPostalCode;contacts.add(contact);
recordsProcessed = recordsProcessed + 1;
}
}
update contacts;
}global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ‘ records processed.’);
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
}}
-
This reply was modified 9 years, 5 months ago by
Kumar.
-
This reply was modified 9 years, 5 months ago by
Log In to reply.