Hi, here we have shown, how to Fetch the size and Count of the records of the opportunity and save the count and size size as Name in Account using batch apex.
Apex controller
Global class optyBatch implements Database.Batchable<Sobject>,Database.stateful{
public integer Count=0;
public static integer size=0;
Global Database.QueryLocator Start(Database.BatchableContext bc){
return Database.getQueryLocator('select id,name from Opportunity');
}
Global void execute(Database.BatchableContext bc,list<Opportunity>Scope){
for(Opportunity opp:Scope){
Count=Count+1;
size=size+1;
}
}
Global void finish(Database.BatchableContext bc){
Account acc = new Account();
acc.Name='Count:'+count+'size:'+size;
acc.phone='1234';
insert acc;
}
}
Output
Open debug Anonymous Window
optyBatch cd = new optyBatch();
Database.executeBatch(cd,10);
here 10 is the batch size…