 Hello, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to discuss about as synchronous apex interview questions and answers. So if you are preparing for interview as a fresher or experience candidate, so I hope these questions and answers will help you. And if you want to learn the concepts in detail, so you can visit studiesalesforce.com. So first question is, what is a synchronous apex? So answer can be an asynchronous process executes a task in the background. User doesn't have to wait for the task to finish. Use asynchronous apex for callouts to external systems, operations that require higher limits, code that needs to run at a certain time. What are the benefits of asynchronous processing? So use efficiency, scalability and higher limits. These are the some benefits which you can have if you implement asynchronous processing in your code. What are types of asynchronous processing? So we have four types, future method, batch apex, curable apex and schedule apex. So future method basically runs in their own thread and do not start until resources are available. So common scenarios are web service callout, batch apex. So it runs large jobs that would exceed normal processing limits. Common scenarios are data cleansing or archiving of reports. Curable apex is similar to future methods, but provide additional job chaining and allow more complex data types to be used. Common scenarios are performing sequential processing operations with external web services. Scheduled apex, so schedule apex to run at a specified time. Common scenarios are for the task that you want to run on daily or weekly basis. You can schedule the apex code with the help of scheduled apex. And if you want to learn each type in detail, so just visit studiesalesforce.com and there you will find the resources. Governor and execution limits in asynchronous processing. So asynchronous apex provides higher governor and execution limits. Number of SOQL is doubled from 100 to 200. Total heap size and maximum CPU time are similarly larger for asynchronous calls. As you get higher limits with Async, also those governor limits are independent of the limits in the synchronous request that are queued the Async request initial. Challenges of asynchronous processing. So ensure fairness of processing and ensure fault tolerance. These two things you need to take care while implementing a synchronous process. What is future apex or future method? So future apex runs process in a separate thread or a later time when system resources become available. Use ad-red future annotation to create future methods. In synchronous processing, all method calls are made from the same thread and no additional processing can occur until the process is complete. Whereas in future method, method runs as synchronously in their own thread. This unblocks users from performing other operations. It provides higher governor and execution limits for processing. When to use future methods. So callouts to external web services to make callouts from a trigger user future or cable method. Process that needs to be executed in a separate or their own thread. Isolating DML operations on different as object types to prevent the mixed DML operation. So these are the requirements for resolving these issues or to implement them. You can use future methods. What are the best practices while using future methods? So you need to ensure future methods execute as fast as possible. In case of web service callouts, try to bundle all callouts together from the same future methods rather than using a separate future method for each callout. Test that a trigger into the future at the future calls is able to handle a trigger collection of 200 records. To process large number of records or synchronously use batch epics instead of future method. Things to remember while using future method. So you need to use at the rate future annotation and your method must be static. Future method can only return a void type. Future method can take primitive data types, area of primitive data types or collection of primitive data types as argument. So it won't work on as objects data type. So future methods cannot take argument objects as argument, right? So it can happen that future methods are running in different order as they are called. You cannot call a future method from a future method. Nor you can invoke a trigger that calls a future method while running a future method. There is a limit of 50 future calls for epics invocation. There is an additional limit on the number of calls in a 24 hours period. So these are the things that you need to remember while using future methods. What is epics? What is batch epics? Sorry. So batch epics runs large epics. Sorry. Batch epics runs large jobs. It can process thousands or millions of records. It processes reports or synchronously in batches for data cleansing or archiving batch epics is probably best solution. How batch epics works. So the execution logic of the batch class is called once for each batch of records that is being processed. Each time when a batch class is invoked, the job is placed on the epics job queue and is executed as a discrete transaction. So advantages of batch epics are every transaction starts with a new set of governor limit. If one batch fails to process successfully, all other successful batches batch transactions aren't global. So these are the advantages of using batch epics. So if you want to work on large number of data, so you can prefer batch epics, which methods are used in batch epics. So batch epics class must implement the database dot batchable interface and include the following three methods. First one is start second is execute and third one is finish. Now you should know the use of each individual method. So what is the use of start method that is available in batch epics. So it collects the records or objects to be passed to the execute method for processing. Start is called once at the beginning of a batch epics job. It returns a database dot query locator object or an iterable that contains the records or objects passed to the job. When query locator object is used, the governor limit for the local, sorry, the total number of reports retrieved by SOQL queries is bypassed and up to 15 million records can be queried. Whereas with an iterable governor limit by SOQL query is enforced. Next is what is the use of execute method in batch epics. So it performs actual processing for each batch of data processed default batch size is 200 record, but you can manipulate that as well batches of reports can execute in any order. It doesn't depends on which order they are received from the start method. It takes a reference to the database dot batchable context object and a list of S object or a list of parameterized types. When using database dot query locator use the returned list. What is the use of finish method in batch epics. So it basically executes post processing operations calls once after all batches are processed. So it execute once similar to start method. So execute method basically runs depending upon number of batches, whereas start and finish methods runs once. For example, sending an email process can be implemented in finish methods. So this is a real example that you can use to answer this batch epics batch practices. So to ensure fast execution of batch jobs, minimize web service callouts time. Tune any SQL query together the records to execute as quickly as possible. So if you write efficient SQL, so your batches will be created accordingly. If you fetch unnecessary data, so your processing time will increase. The longer the batch job executes, the more likely other queued jobs are delayed when many jobs are in the queue. So you need to take care about this as well. Use batch epics when more than one batch of records are there. In case of one batch, you can prefer queueable epics. Minimize the number of errors in Corona's request. If you are planning to invoke a batch job from a trigger, then you must be able to guarantee that the trigger won't add more bad jobs than the limit. So these are the things that you need to remember as part of best practices while you are implementing batch epics. And these can be asked in an interview as well. What is queueable epics? So superset of future methods with extra benefits, combination of future methods and batch epics, works beyond primitive arguments, called by a simple system.engageob method. And engageob method basically returns a job ID that can be monitored. So basically it has additional features over future method. So it depends like as per the requirement, you can prefer future method as well as you can use queueable epics as well. So you just need to know what is available with future method and what is with queueable epics. So these are the benefits which is available with queueable epics. So you can use non-primitive types as well. You can monitor the jobs and you can change the jobs. Next question, important to remember while using queueable epics. So the execution of a queue job counts once against the shared limit for asynchronous epics method executions. You can add up to 50 jobs to the queue with system.engageob in a single transaction. When changing jobs, you can add only one job from an executing job with system.engageob. No limit is enforced on the depth of change jobs. Note the developer edition and trial logs, the maximum stack depth for change job is five. What is scheduled epics? So you can run epics classes at a specified time through scheduled epics. You can run maintenance tasks on daily or weekly basis, implements schedulable interface and epics class. If you want to implement schedule epics, we need to implement this interface. How many ways are there to schedule epics? So using the system.schedule method through crone expression. So this is the syntax, you need to write system.schedule. Then first parameter is the job name. Second is the crone expression that you need to create and then the schedule class instance. And second way is like through UI, you can go to setup and in the quick find you can search for epics classes and then you can just click on the schedule epics button. There you will be picking particular class which you want to schedule. So these are the two ways to schedule epics. And if you want to learn this in detail like how we can implement it. So just visit study salesforce.com there you will find the resources. What is crone expression? So crone expression is basically a string of five or six fields that represents a set of times. Normally as a schedule to execute some routine. So this is the example. So here you can see I have colored these things. So 20, 30, 8, 10, 2, 6, 2, 0, 2. So 20 is representing seconds, 30 minutes, 8 hours, 10 day of month, 2 month. So one is for Jan accordingly. Then six is day of week. So one for Sunday and accordingly and 2022 is year. So it is optional. So null or the range you can prefer things to remember while scheduling epics. So you can only have 100 schedule jobs at one time while scheduling a class from a trigger. Then you must guarantee that the trigger won't add more scheduled jobs than the limit. Synchronous web service callouts are not supported from scheduled epics. Make an asynchronous callout by placing the callout in a method annotated with adrid future callout equals to two and call this method from scheduled epics. If scheduled epics executes a batch job then callouts are supported from the batch class. So these are some interview question and answers related to asynchronous epics. If you want to learn these concepts in detail so you can visit studiesaleschools.com. I hope these question and answers will help you to prepare this topic.