Activity Forums Salesforce® Discussions Returning no. of records processed by a batch class in Salesforce

  • Shekhar Gadewar

    Member
    December 15, 2016 at 7:11 am

    Have you tried using database.stateful in that class?

    Do let me know if any help needed in it.

    Regards,

    Shekhar

  • Kumar

    Member
    December 15, 2016 at 7:56 am

    Thanks ! I will try to implement database.stateful

    • This reply was modified 8 years ago by  Kumar.
  • Kumar

    Member
    December 15, 2016 at 12:34 pm

    I got it by using database.stateful.

    Thanks again !

  • Shekhar Gadewar

    Member
    December 16, 2016 at 10:06 am

    Great!

    Please share the code snippet with the group so it will help others too.

     

  • Kumar

    Member
    December 16, 2016 at 10:14 am

    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 8 years ago by  Kumar.
  • Shekhar Gadewar

    Member
    December 16, 2016 at 10:18 am

    Awesome work mate!

     

     

Log In to reply.

Popular Salesforce Blogs