You need to sign in to do that
Don't have an account?
balakrishna 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
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
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
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
Please mark my answer as a best solution to your question and Like it to help others if it solves your problem.