[0:00]Hey everyone, it's great to have you back on SFDC Ninja. In today's video I'll be talking about interview questions related to Batch effects.
[0:08]So without wasting any more time, let's get started. So first start with a very basic question, why do we use batch class?
[0:13]Guys, if we have a requirement in which we need to process millions of records, then it would be difficult to do this synchronously.
[0:20]So we use Batch Effects to process bulk data or records with a new set of Governor Limits per transaction.
[0:26]Now a question arise that what is the meaning of transaction?
[0:29]So guys, a transaction represents a set of operations that are executed as a single unit.
[0:35]And a Batch Apex executes with the new set of Governor limits per transaction.
[0:39]Here the thing is, Governor limits gets reset for each transaction and this thing allows the Batch Effects to process a large number of records without hitting the Governor limits.
[0:48]Let's move to next question, which is why to use Batch class as we already have data loader to process the bulk data?
[0:55]I'm totally agree with this point that data loader can handle mass records.
[0:59]But data loader works on a static or predefined data sets, or if we have a requirement in which we can do operations by using the data present in Excel sheets.
[1:07]And we will go for Batch Effects if we have to perform some complex logic or some complex calculation or call out.
[1:15]So we use data loader if there is a need to work on a static data set or on a data which is present in Excel sheet.
[1:22]Let's move to next question, which is what is database.Batchable?
[1:27]So in simple terms, it is an interface provided by Salesforce.
[1:31]Now, to create a batch class, you have to implement this interface and this interface contains three methods that must be implemented.
[1:39]And these methods are start, execute and finish. We will talk about each method one by one, so let's begin with start method.
[1:44]Start method provides the data to work on.
[1:47]Basically, it is used to collect records to be passed to execute method.
[1:52]It returns either database.QueryLocator object or an iterable that contains records.
[1:57]Now, after getting data from start method, we perform all of our operations in execute method and at last in finish method.
[2:04]We do all of our post-processing operations, like sending an email or some other operation.
[2:10]And there is one more important point that we can leave finish method empty, but it is required and we have to write it in our batch class.
[2:17]Next question is what is the difference between database.QueryLocator and Iterable<sObject> used in Batch Apex?
[2:23]Guys, see in database.QueryLocator, the Governor limit for the total number of records retrieved by SOQL is bypassed and you can query up to 50 million records.
[2:33]Basically, we use it when we need records which can be filtered by SOQL.
[2:37]I mean, when we can fetch data by using SOQL query only.
[2:40]For example, let's say, we will fetch all inactive accounts using SOQL in start method and then we will update them in execute method.
[2:48]Or we will first fetch all old opportunity records and after that, we will delete them in execute method.
[2:54]On the other hand, when you have a complex use case in which SOQL cannot be constructed.
[3:00]Then in that case, we will go with Iterable.
[3:03]For example, let's say a batch accepts account records where each account is having more than two contacts and each contact's mailing address should not be same.
[3:11]So in this type of complex scenarios, we will go with Iterable.
[3:15]There is one more important point that Governor limit for the total number of records is still enforced if you are using Iterable.
[3:21]Next question is what is BatchableContext?
[3:24]So basically, this interface represents the parameter type of a batch method and it contains the batch job ID, which you can use to track batch job.
[3:33]Let's move to next question, what is scope in Batch Apex?
[3:37]So in Batch Apex, scope refers to two interrelated concepts.
[3:40]First, it is the set of records on which your batch job will iterate and perform operations.
[3:45]Second, start method in batch class retrieve the records and returns them.
[3:50]Then the execute method, which perform the main processing on each record received them in smaller chunks to manage the governor limits.
[3:57]So this chunk size is the specific scope you define in database.execute method.
[4:01]Let's move to next question, what is Database.Stateful?
[4:05]Basically, Batch Apex is stateless. Each execution of a Batch Apex is considered a discrete transaction.
[4:10]For example, let's say a batch job contains 1,000 records and it is executed without the scope parameter.
[4:17]Then it is considered five different transactions for 200 records each.
[4:21]Now, if you are using database.stateful, then instance member variables can retain their values between transactions.
[4:27]Guys, see in Batch Apex, each execution gets a fresh copy of variables and objects.
[4:32]Why? Because batch class by default is stateless.
[4:35]Now, if you specify database.stateful, then instance member variables can retain their values between transactions.
[4:42]But please remember that static member variables do not retain their values and gets reset between transactions.
[4:48]Now the question is, what is the use of maintaining state?
[4:52]So it can be used in scenarios. Let's say you have a batch class in which accounts are getting updated.
[4:57]Now we can use an instance variable to store the number of accounts updated.
[5:01]So in this type of scenarios, we use database.stateful in which we want to track that how many records are processed or updated.
[5:08]Next question is how to execute a Batch? So you can use database.executeBatch to initiate or execute a batch job.
[5:16]Basically, it takes two parameters. First is an instance of your batch class and second is an optional scope or we can say chunk size, which specified the number of records to pass into execute method.
[5:27]Guys, this scope parameter can have a maximum value of 2,000.
[5:31]And there is one more important point that database.executeBatch method return the ID of ASYNC Effects Job object, which you can then use to track the progress of job.
[5:39]Next question is how to monitor a Batch Job?
[5:42]So you can monitor status of job manually, for that you just need to go to Apex Job page from setup, or you can track your batch job using the job ID returned by database.executeBatch method.
[5:53]I already told you that it returns the ID of a sync effects job object, which you can use to track the progress.
[5:58]Next question is if a batch is having 200 records and one record fails, then what will happen?
[6:03]So if any record fails, then the whole batch will be rolled back, but other batches will get executed.
[6:09]Next is, how many times the execute method will be executed to process the 1236 records?
[6:16]So guys, it depends on your batch size that you have configured at the time of calling batch class.
[6:21]Basically the formula is, execution count is equal to total number of records divided by batch size.
[6:29]So if you have specified batch size to 400, then the execute method will be called four times.
[6:33]Next question is, can we call a batch from another batch?
[6:36]So yes, we can call another batch class, but only in finish method.
[6:40]If we try to call it in start or execute method, then we will get system.sync exception.
[6:45]Next is, can we call future method from Batch Apex?
[6:48]So no, it is not possible to call future method from batch, because there is a limitation that no more than 50 future methods can be invoked in a single Apex transaction.
[6:57]But work around is, you can use web services to call future method from batch.
[7:01]Next is, how many Batch jobs can be queued or active concurrently?
[7:05]So up to five batch jobs can be queued or active at the same time.
[7:09]Next is, can we call the batch Apex from triggers?
[7:12]So yes, we can call a batch Apex from trigger, but you have to take care that the trigger does not add more batch jobs than the limit.
[7:19]This is because of the reason that we can only have five Apex jobs queued or executed at the same time.
[7:24]Also, if we are invoking a batch from trigger, then the Governor limits will come into picture and there will also be performance issue because of additional processing.
[7:32]So it is not a recommended way to call batch from triggers.
[7:35]Next is, can we do callout from Batch Apex?
[7:38]Yes, we can do callout from Batch Apex, but for that, we have to implement database.allowsCallouts interface in our batch class.
[7:44]Next is, how many times start, execute and finish methods will execute in Batch Apex?
[7:50]So start and finish method will execute one time.
[7:53]And for execute method, it depends on batch size and data retrieved in start method.
[7:58]Next is what is the Batch execution limit per day?
[8:01]So the maximum number of batch Apex method executions per 24-hour period is 2 lakh 50,000.
[8:06]Next is, how can you stop a Batch job?
[8:09]So you can use system.aboutJob method by passing job ID to stop a batch job.
[8:14]Next question is what is Apex Flex Queue?
[8:17]Basically, Salesforce allows five batch job to be running or to be queued.
[8:21]So if you have consumed all these five limits and then again if system has received one more batch effects execution request, then all these waiting batch will be stored in Apex Flex Queue.
[8:31]The maximum number of batch class allowed in Apex Flex Queue for execution is 100.
[8:36]Next is, can we change the order of already queued Batch jobs?
[8:39]So yes, we can change the order, but for only jobs with status holding.
[8:44]It can be done through UI of Apex Flex Queue, or you can use Apex Flex Queue methods.
[8:48]Next is, can we schedule a Batch Apex?
[8:52]So yes, you can schedule a Batch Apex through system.scheduleBatch method.
[8:56]Next is, can we call Queueable from a Batch?
[8:59]So yes, we can call it, but there is a limitation to just one call per execute in Batch Apex.
[9:04]Next is, can we query related records using Database.QueryLocator?
[9:08]So yes, we can do subquery for related records, but if you are using a subquery, then the batch job processing becomes slower.
[9:14]So a better work around is to fetch the related records in execute method by using the ID returned from start method.
[9:21]Next is, how to publish platform events in Batch class?
[9:25]So there is a method which is database.registerPlatformEvents. Using it, we can publish platform events from batch class.
[9:32]So these all are the questions. That's it for today guys and I'll be back with more interesting videos. Thank you and keep watching.



