[0:00]Hello everyone. It's great to have you back on SFDC Ninja. In today's video, I'll be talking about interview questions related to future apex, so without wasting any more time, let's get it started. So, let's start with the very first question. Why do we use Future Apex? We use future effects in case of long-running operations, like collouts to external web services. And there is one more scenario in which we commonly use future effects, which is preventing mixed DML error. Now, the question arise that what is mixed DML error? So, before understanding mixed DML error, we should understand that what are setup and non-setup objects. So, in simple terms, setup objects are the objects in which if you are making any changes, then it will affect salesforce all security. For example, profile and permission sets. These objects are basically define the configuration and setting of your salesforce org. So, these are setup objects and non-setup objects are simply standard and custom objects. Like account, contact, and any other custom object. Basically, these objects represent your business data. So, now we know that what are setup and non-setup object. Now, let's understand mixed DML error. So, in Salesforce, mixed DML error occurs when you try to make a DML on both setup and non-setup object in a single transaction. Let's say you have an Apex trigger that fires on creation of a new account record. Now, inside that trigger, you need to perform two tasks. First, you need to create a new contact associated with that account, and second, you need to update account's owner, which is user object. So, here contact is a non-setup object, and account owner, which is user is a setup object, and you are performing DML on these both in a single transaction. So, in this case, you will get a mixed DML error, and we use future effects to prevent this type of error. Now, let's move to next question, which is how to create a future method. So, if you want to create a future method, then you just need to annotate a method with @Future annotation. Next question is, what are the considerations while creating future method? So, there are some considerations for future method, like future method must be static. Second, it can only return a void type. Third, you cannot pass S object as parameter in future method. So, these are the few points which a developer should remember while creating a future method. Now, there are few questions which arise from these points. We will talk about them one by one. Like, why future method must be static? So, all future method must be static. This is to ensure that the method runs independently. And future method are not tied to any specific instance of class, which means that future method operate without relying on any instance variable or methods. That's why future method must be static. Next question is, why future method returns void? So, future method returns void because of its asynchronous nature of execution. See, future methods run separately from rest of your code. That's why there is no way for them to instantly give a result. That's why future method returns void. Next question is, why we cannot pass S object as parameter in future method? So, the reason is that because we are not sure that when it will get executed. And there may be chances that whenever it gets executed, it gets the older values because maybe the value has been changed somewhere else in effects. So, that's why we cannot pass S object as parameter in future method. Guys, in previous question, I told you that we cannot pass S object as parameter. That's why there is another question that what could be the workaround of that limitation. So, workaround could be that you can pass list of IDs as parameter, and then you can query records based on those IDs. So, this could be the workaround. Let's move to next question, which is how can we perform callouts from Future method? So, guys, if you want to perform callout in future method, then you have to use an extra parameter with @Future annotation, which is callout equals to true, which indicates that callouts are now allowed. Let's move to next question, which is how can we call future method? So, you can invoke a future method, the same way you invoke any enable Apex method, like you call class name dot method name. Let's move to next question, which is how many future methods can be defined in a class? So, there is no such restriction. You can define any number of future method in a class. Let's move to next question. Can we call future method from Batch Apex? So, in previous video also, I told you that you cannot directly call future method from Batch Apex, but workaround could be you can use web service for it. Next is, how can we monitor Future Apex? Guys, as you know that future methods do not return any ID. So, we cannot monitor it directly, but if you want to monitor it, then there is a object which is Async Apex job object, you can query on it, and you can use filters like method name or job type to find your job. Next is, can we call the Future Apex from triggers? So, yes, we can call a future method from triggers. It is basically used to do callouts from triggers. So, yes. Next is, can we call Future method from another Future method? So, no, you cannot invoke a future method from another future method. Next is, what are the best practice of future method? So, some of the best practices of future methods are, you should avoid adding large number of future methods to asynchronous queue. Because if more than 2,000 unprocessed requests from a single organization are in the queue, then any additional request from the same organization will be delayed, and the queue will handle requests from other organization. Second, you must ensure that future methods executes as fast as possible. Third, if there is a need to process large number of records, then you should go with batch Apex instead of future methods. Next is, what is the future executions limit per day? So, the maximum number of future method invocation per 24-hour period is 250,000. So, these all are the questions related to future apex. If I missed something, then you can comment that question. That's it for today, guys. Thank you, and keep watching.
Watch on YouTube
Share
MORE TRANSCRIPTS



