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
balakrishna mandula 6balakrishna mandula 6 

No of records processed in Batch apex using Stateful interface

Hi,

I want to know the no.of  records processed/updated in Batch apex. I know by implementing Database.stateful it can be achieved. I don't know how to do it.

e.g.,
There are 1500 records in Opportunity object I try to edit one field based on some condition. Some of the records ate not meeting the criteria, so not being updated. I want know the exact no.of of records which are updated.

Thanks in advance
Bala
Best Answer chosen by balakrishna mandula 6
Aakaash NairAakaash Nair
Hi Bala,

This could help.

global class AAAAA0 implements Database.Batchable<sObject>, Database.Stateful {
global Database.QueryLocator start(Database.BatchableContext BC){
System.debug('★Start');
try {
String soql = '';
soql += 'select ProfileId from user where UserName = \'aaaaa@cs5.demo\'';
return Database.getQueryLocator(soql);
} catch (Exception ex) {
System.debug('Error:' + ex);
System.debug('aaaaa Start stackTrace: ' + ex.getStackTraceString());
throw ex;
}
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
System.debug('★★execute');
try {
List<User> updateList = new List<User>();
User user2 = [select ProfileId from user where UserName = 'aaaaa@cs5.demo'];
for (sObject sObj : scope) {
User user1 = (User)sObj;
user1.ProfileId = user2.ProfileId;
updateList.add(user1);
}
update updateList;
} catch (Exception ex) {
System.debug('Error:' + ex);
System.debug('aaaa Exe stackTrace: ' + ex.getStackTraceString());
throw ex;
}
}
global void finish(Database.BatchableContext BC){
System.debug('★★★finish');
try {
Contact contact1 = [select FirstName, AccountId from contact where LastName = 'Contact1'];
Contact contact2 = [select FirstName, AccountId from contact where LastName = 'Contact2'];
contact1.AccountId = contact2.AccountId;
update contact1;
} catch (Exception ex) {
System.debug('Error:' + ex);
System.debug('FUSUI Fin stackTrace: ' + ex.getStackTraceString());
throw ex;
}
}
}
==================
Test Class:
=========
@isTest
private class AAAAASetAdminAuthorityTest0 {
static testMethod void SetAdminAuthority01() {
// try {
Test.startTest();
Database.executeBatch(new AAAAASetAdminAuthority0(), 1000);
Test.stopTest();
// } catch(Exception ex) {
// System.debug('aaaaaa:' + ex.getMessage());
// }
}
}

Let me know if this help!

Thanks,
Aakaash

All Answers

Aakaash NairAakaash Nair
Hi Bala,

This could help.

global class AAAAA0 implements Database.Batchable<sObject>, Database.Stateful {
global Database.QueryLocator start(Database.BatchableContext BC){
System.debug('★Start');
try {
String soql = '';
soql += 'select ProfileId from user where UserName = \'aaaaa@cs5.demo\'';
return Database.getQueryLocator(soql);
} catch (Exception ex) {
System.debug('Error:' + ex);
System.debug('aaaaa Start stackTrace: ' + ex.getStackTraceString());
throw ex;
}
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
System.debug('★★execute');
try {
List<User> updateList = new List<User>();
User user2 = [select ProfileId from user where UserName = 'aaaaa@cs5.demo'];
for (sObject sObj : scope) {
User user1 = (User)sObj;
user1.ProfileId = user2.ProfileId;
updateList.add(user1);
}
update updateList;
} catch (Exception ex) {
System.debug('Error:' + ex);
System.debug('aaaa Exe stackTrace: ' + ex.getStackTraceString());
throw ex;
}
}
global void finish(Database.BatchableContext BC){
System.debug('★★★finish');
try {
Contact contact1 = [select FirstName, AccountId from contact where LastName = 'Contact1'];
Contact contact2 = [select FirstName, AccountId from contact where LastName = 'Contact2'];
contact1.AccountId = contact2.AccountId;
update contact1;
} catch (Exception ex) {
System.debug('Error:' + ex);
System.debug('FUSUI Fin stackTrace: ' + ex.getStackTraceString());
throw ex;
}
}
}
==================
Test Class:
=========
@isTest
private class AAAAASetAdminAuthorityTest0 {
static testMethod void SetAdminAuthority01() {
// try {
Test.startTest();
Database.executeBatch(new AAAAASetAdminAuthority0(), 1000);
Test.stopTest();
// } catch(Exception ex) {
// System.debug('aaaaaa:' + ex.getMessage());
// }
}
}

Let me know if this help!

Thanks,
Aakaash

This was selected as the best answer
Deepak Kumar ShyoranDeepak Kumar Shyoran
You can use AsyncApexJob object to trace those records.

ID batchprocessid = Database.executeBatch(JobName);

AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors
                    FROM AsyncApexJob WHERE ID =: batchprocessid ];
Please mark my answer as a best solution to your question and Like it to help others if it solves your problem.