The following practice questions are for Salesforce developers who are new to Salesforce and wish to solve some basic programming questions. Using batch Apex classes, you can process records in batches asynchronously. If you have a large number of records to process, for example, for data cleansing or archiving, batch Apex is your solution. Each invocation of a batch class results in a job being placed on the Apex job queue for execution. Write or Schedule Apex Batch classes for the following.
1. Write an Apex Batch class in your Salesforce developer org to delete Accounts created in last 10 days.
Click here to learn more
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = ‘SELECT Id,CreatedDate FROM Account WHERE CreatedDate <= N_DAYS_AGO:10’;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
/*List <Account>acclist = new List<Account>();
for(Account acc:acclist){
//string test = acc.Name;
acclist.add(acc);
}*/
system.debug(‘**** Acc List–‘+ scope);
delete scope;
}
global void finish(Database.BatchableContext BC)
{
system.debug(‘**** Finish’);
}
}
global class ScheduleDelete implements Schedulable{
global void execute(SchedulableContext sc) {
AccountDelete a = new AccountDelete();
database.executebatch(a);
}
}
2. Schedule the above class to run twice every day at 12:00 PM.
Click here to learn more
ScheduleDelete bt2 = new ScheduleDelete();
System.schedule(‘every 12 hour Batch Schedule’, CRON_EXP1, bt2);
3. Write a Script to delete the above Schedule Jobs.
Click here to learn more
String CRON_EXP1 = ‘0 0 12,0 * * ?’ ;
ScheduleDelete bt2 = new ScheduleDelete();
ID jobId = System.schedule(‘every 12 hour Batch Schedule’, CRON_EXP1, bt2);
system.debug(‘++++++++++’+jobId);
system.abortJob(jobId);
4. Create Batch class to create 5 new Opportunities daily.
Click here to learn more
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = ‘SELECT Id,Name FROM Opportunity LIMIT 1’;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
List <opportunity> oppnew = new List<opportunity>();
for(integer i=1;i<6;i++){
Opportunity opp = new Opportunity();
opp.name = ‘Test ‘ + i;
opp.StageName = ‘Prospecting’;
opp.CloseDate = Date.Today();
oppnew.add(opp);
}
system.debug(‘**** Opp List–‘+ oppnew);
insert oppnew;
}
global void finish(Database.BatchableContext BC)
{
system.debug(‘**** Finish’);
}
}
global class OpportunityBatchScheduler implements Schedulable{
global void execute(SchedulableContext sc) {
OpportunityBatch b = new OpportunityBatch();
database.executebatch(b);
}
}
5. Schedule the above class to run 4 times every day at after every 6 hours.
Click here to learn more
OpportunityBatchScheduler bt1 = new OpportunityBatchScheduler();
System.schedule(‘six hour Batch Schedule job1’, CRON_EXP1, bt1);
6. Write a Script to delete the above Schedule Jobs
Click here to learn more
OpportunityBatchScheduler bt1 = new OpportunityBatchScheduler();
Id jobId = System.schedule(‘six hour Batch Schedule job1’, CRON_EXP1, bt1);
system.debug(‘+++++++++’ +jobId)
system.abortJob(jobId);
7. Create Batch to update the Account “Count” field(Create this field) with sum of number contacts related to that Account. This can be done by Summary formula field but still we will try to achieve this by batch class.
Click here to learn more
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = ‘SELECT Id,Name,(SELECT Id FROM Contacts) FROM Account LIMIT 5’;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
list<Account> accList = new list<Account>();
for(Account acc: scope){
acc.count__c = acc.Contacts.size();
accList.add(acc);
}
system.debug(‘****accList — ‘ + accList);
update accList;
}
global void finish(Database.BatchableContext BC)
{
system.debug(‘**** Finish’);
}
}
Countbatch b = new Countbatch();
database.executebatch(b);
8. Write the Script to Schedule the above class to run in every 5 minutes.
Click here to learn more
String CRON_EXP1 = ‘0 5 * * * ?’ ;
String CRON_EXP2 = ‘0 10 * * * ?’ ;
String CRON_EXP3 = ‘0 15 * * * ?’ ;
String CRON_EXP4 = ‘0 20 * * * ?’ ;
String CRON_EXP5 = ‘0 25 * * * ?’ ;
String CRON_EXP6 = ‘0 30 * * * ?’ ;
String CRON_EXP7 = ‘0 35 * * * ?’ ;
String CRON_EXP8 = ‘0 40 * * * ?’ ;
String CRON_EXP9 = ‘0 45 * * * ?’ ;
String CRON_EXP10 = ‘0 50 * * * ?’ ;
String CRON_EXP11 = ‘0 55 * * * ?’ ;
String CRON_EXP12 = ‘0 0 * * * ?’ ;
CountbatchSchedule bt1 = new CountbatchSchedule();
CountbatchSchedule bt2 = new CountbatchSchedule();
CountbatchSchedule bt3 = new CountbatchSchedule();
CountbatchSchedule bt4 = new CountbatchSchedule();
CountbatchSchedule bt5 = new CountbatchSchedule();
CountbatchSchedule bt6 = new CountbatchSchedule();
CountbatchSchedule bt7 = new CountbatchSchedule();
CountbatchSchedule bt8 = new CountbatchSchedule();
CountbatchSchedule bt9 = new CountbatchSchedule();
CountbatchSchedule bt10 = new CountbatchSchedule();
CountbatchSchedule bt11 = new CountbatchSchedule();
CountbatchSchedule bt12 = new CountbatchSchedule();
System.schedule(‘updateAccount count every 5 minutes’,CRON_EXP1,bt1); System.schedule(‘updateAccount count every 10 minutes’,CRON_EXP2,bt2); System.schedule(‘updateAccount count every 15 minutes’,CRON_EXP3,bt3); System.schedule(‘updateAccount count every 20 minutes’,CRON_EXP4,bt4); System.schedule(‘updateAccount count every 25 minutes’,CRON_EXP5,bt5); System.schedule(‘updateAccount count every 30 minutes’,CRON_EXP6,bt6); System.schedule(‘updateAccount count every 35 minutes’,CRON_EXP7,bt7); System.schedule(‘updateAccount count every 40 minutes’,CRON_EXP8,bt8); System.schedule(‘updateAccount count every 45 minutes’,CRON_EXP9,bt9); System.schedule(‘updateAccount count every 50 minutes’,CRON_EXP10,bt10); System.schedule(‘updateAccount count every 55 minutes’,CRON_EXP11,bt11); System.schedule(‘updateAccount count every 60 minutes’,CRON_EXP12,bt12);
9. Write the above script in 10 line of code only: HINT: Use For loop
Click here to learn more
String CRON_EXP = ‘0 ‘+i+’ * * * ?’ ;
CountbatchSchedule bt1 = new CountbatchSchedule();
System.schedule(‘updateAccountEvery5Minutes’+i,CRON_EXP,bt1);
}
10. Write a Script to delete the above Schedule Jobs with for loop
Click here to learn more
String CRON_EXP = ‘0 ‘+i+’ * * * ?’ ;
CountbatchSchedule bt2= new CountbatchSchedule();
Id jobId=System.schedule(‘updateAccountCount’+i,CRON_EXP,bt2);
System.abortJob(jobId); }
September 15, 2015
Hi Ajay,
I want more examples in batchapex will u please add your portal.
Awaiting for your posts.
Regards,
Prabu
December 29, 2015
Hello Sir,
I am newbie just learning salesforce and your blog really help me to understand the concepts in easy way.Please keep post such kind of post.Really appreciate your work.
Thanks.
Bonny
July 23, 2018
Hi Ajay,
Can u provide its test class? Coz I have tried all the things it’s not covering for me.
/*********Batch Class**********/
public class Batch_Delete_ErrorLog implements Database.Batchable {
public Database.QueryLocator start(Database.BatchableContext BC){
try{
string query = ‘SELECT Id FROM clcommon__Log__c WHERE createDdate < LAST_N_Days:15';
return Database.getQueryLocator(query);
}catch(Exception e){
HandleBusinessException.captureError('Batch_Delete_ErrorLog', 'start method', e);
return null;
}
}
public void execute(Database.BatchableContext BC, List logList){
try{
if(! logList.isEmpty())
DELETE logList;
Database.EmptyRecycleBin(logList);
}
catch(Exception e){
HandleBusinessException.captureError(‘Batch_Delete_ErrorLog’, ‘execute method’, e);
}
}
public void finish(Database.BatchableContext BC){
}
}
/****************Test Class*****************/
@isTest
private class Batch_Delete_ErrorLogTest {
private static testMethod void batchTest(){
clcommon__Log__c clLog = new clcommon__Log__c();
clLog.Class_Name__c = ‘Test’;
clLog.clcommon__Type__c = ‘System.ListException’;
clLog.CreatedDate = Date.today().addDays(-15);
INSERT clLog;
Test.setCreatedDate(clLog.Id, Date.today().addDays(-15));
System.assert(clLog != null);
}
private static testMethod void method3(){
Test.startTest();
Batch_Delete_ErrorLog b = new Batch_Delete_ErrorLog();
Id batchId = Database.executeBatch(b);
System.assert(batchId != null);
Test.stopTest();
}
}