Monday, December 23, 2019

Batch Apex - Interview Questions With Answers

1. How many active batches(running parallel) can be allowed at a time?

Salesforce by default allows 5 active batches running at a time and other batches will be in queue for running

2. How can we schedule batch class to run in future only once from the specified minutes from current time.

public static String scheduleBatch(Database.Batchable batchable, String jobName, Integer minutesFromNow)

or

public static String scheduleBatch(Database.Batchable batchable, String jobName, Integer minutesFromNow,Integer batchSize)

3.How to calculate the batch size if we are making any call out from the batch class execute method?

Basically in salesforce we have limitation of 100 call outs for a transaction.When it comes to batch class each execute method call will be considered as one transaction.So,in this case your batch size must be calculated in such way

   Batch size = (No of callouts allowed by salesforce for a single transaction /total number of call outs for each record) - 1;

   Batch size = (100 /total number of call outs for each record) - 1;


4. How to stop all the running batches related to a particular batch classes before scheduling the same batch class from the schedule class.


global class CaseCreationonSchedular implements Schedulable
{
    global void execute(SchedulableContext SC)
    {
        
        for(AsyncApexJob ap: [SELECT Id, ApexClass.Name,createddate, JobType, Status, JobItemsProcessed, MethodName, ParentJobId FROM AsyncApexJob Where ParentJobId!=Null AND JobType IN('BatchApex','BatchApexWorker') And ApexClass.Name='YourBatchClassName' And Status NOT IN('Completed','Aborted')])
        {
          System.abortJob(ap.ParentJobId);

        }
        YourBatchClassName cls = new YourBatchClassName();
        DataBase.executeBatch(cls);
    }
}

5.How can we schedule a batch class to run every 10 or 5 or 2 mins.

Salesforce haven't provided wild card entries (* every,- range,/,?) in place of minutes and seconds.So,if you want schedule a class to run every  10 minutes you have to schedule same class 6 times (60min/6) with diff batch name ,for 5 mins 12 times and for 2 mins 30 times..As the time interval keeps going down schedule the same class goes up which is an irritating thing to monitor or even it's very hard to handle these many jobs.

For this we can simply do this in the code so that at any given point of time you will have only schedule job running for the same.


global class CaseCreationonSchedular implements Schedulable
{
    public void execute(SchedulableContext scon)
    {
        System.abortJob(scon.getTriggerId()); // abort already running schedule job 
        Decimal nextInterval = System.Label.nextIntervalTime; // config to decide next interval 2,5 or 10 mins etc..
        System.schedule('CaseCreationonBatch - '+String.valueOf(DateTime.now()), '0 '+DateTime.now().addMinutes(Integer.valueOf(nextInterval)).minute()+' */1 ? * *', this); //Schedule the class to run with specified time
  
        Database.executeBatch(new YourBatchClassName()); // Call you batch class 
       
    }
}

6.Why to use Batch class as we already having data loader to process the bulk data.

Agree with this point if and only if the data needs to be updated is static or predefined or which can be done through excel.

We will choose batch class if we have to perform some custom calculations at run time or if you want to run some complex logic which can't be driven by excel sheets in those cases we have to go with batch classes.

Examples:

  • Do some relationship quires to update the data.
  • Make a call out to get the some information related to each record.
7.How many times the execute method will be executed to process the  1234 records.

It depends on your batch size what you have configured at the time of calling the batch class from schedule class.

Execution method count = Total No Of Records/Batch Size (Any decimal ceil it to upper value)
  • If you haven't set any batch size then - 1234/200 = 6.17 = 7 times execute method will be called

8.What is the maximum size of a batch that we can set up ?

2000

9.What is the minimum batch size we can set up is?

1

10.What is the default size of batch if we haven't configured at the time of execution?

200

11.What is Apex Flex Queue?

At a time salesforce allolws 5 batches to be running or to be in queued state.So,if you have consumed all these 5 limits and if system has received one or more batch execution request all these waiting batch will be stored in this Apex Flex Queue.

12. What is the maximum number of batch classes allowed in Apex Flex Queue for execution?

100

13.How can we do chaining of batch jobs?

we can do this by simply calling the chained batch/second batch class from the finish method of the first batch class.

Why to call only from the finish method why not from execute?because the execute method will gets invoked multiple times based on the volume of the records and batch size.So,if your calling it from execute method then the chaining class will get called multiple times which is not an suggested way of doing.

We can also use the Queueable Apex for chaining of the jobs.


Hope this helps you..Enjoy..!

13 comments: