Future apex

Future Methods in Apex | The Salesforce Developer Guide

Future Apex is utilized to run measures in a different string, sometime in the not too distant future when framework assets become accessible. 

Note: Technically, you utilize the @future explanation to recognize strategies that run nonconcurrently. Nonetheless, on the grounds that "strategies related to the @future comment" is arduous, they are generally alluded to as "future techniques" and that is the way we'll reference them for the rest of this module

When utilizing simultaneous handling, all strategy calls are produced using the very string that is executing the Apex code, and no extra preparation can happen until the cycle is finished. You can utilize future strategies for any activity you'd prefer to run nonconcurrently in its own string. This gives the advantages of not impeding the client from performing different activities and giving higher lead representative and execution limits for the cycle. Everybody's a victor with offbeat preparation. 

Future strategies are commonly utilized for: 

Callouts to outside Web administrations. In the event that you are making callouts from a trigger or in the wake of playing out a DML activity, you should utilize a future or queueable strategy. A callout in a trigger would hold the information base association open for the lifetime of the callout and that is a "no-no" in a multitenant climate

Activities you need to run in their own string, when time allows, for example, some kind of asset serious figuring or preparing of records

Separating DML procedure on various sObject types to forestall the blended DML blunder. This is fairly an edge-case however you may every so often stumble into this issue. See sObjects That Cannot Be Used Together in DML Operations for additional subtleties.

dont miss out iconDon't forget to check out: Schedule Apex in Salesforce | Apex Developer Guide

Future Method Syntax

Future strategies must be static techniques, and can just restore a void sort. The predefined boundaries must be crude information types, varieties of crude information types, or assortments of crude information types. Quite, future strategies can't accept standard or custom articles as contentions. A typical example is to pass the strategy a List of record IDs that you need to measure nonconcurrently.

global class SomeClass {
    public static void someFutureMethod(List<Id> recordIds) {
        List<Account> accounts = [Select Id, Name from Account Where Id IN :recordIds];
        // process account records to do awesome stuff

Best Practices

Since each future strategy conjuring adds one solicitation to the nonconcurrent line, dodge configuration designs that add huge quantities of future solicitations throughout a brief timeframe. On the off chance that your plan can possibly add at least 2000 demands all at once, solicitations could get deferred because of stream control. Here are some accepted procedures you need to remember: 

Guarantee that future strategies execute as quickly as could reasonably be expected. 

On the off chance that utilizing Web administration callouts, attempt to package all callouts together from a similar future strategy, instead of utilizing a different future technique for each callout

Lead careful testing at scale. Test that a trigger enqueuing the @future calls can deal with a trigger assortment of 200 records. This decides whether deferrals may happen given the plan at current and future volumes. 

Consider utilizing Batch Apex rather than future strategies to handle an enormous number of records nonconcurrently. This is more effective than making a future solicitation for each record.

Things to Remember

Future strategies are an extraordinary device, yet with incredible force comes incredible duty. Here are a few things to remember when utilizing them: 

Strategies with the future explanation must be static techniques, and can just restore a void sort. 

The predefined boundaries must be crude information types, varieties of crude information types, or assortments of crude information types; future techniques can't accept objects as contentions. 

Future techniques won't really execute in a similar request they are called. Moreover, it's conceivable that two future strategies could run simultaneously, which could bring about record locking if the two techniques were refreshing a similar record. 

dont miss out iconCheck out another amazing blog by Sumit here: Using Batch Apex | Apex Developer Guide | Salesforce

Future techniques can't be utilized in Visualforce regulators in getMethodName(), setMethodName(), nor in the constructor. 

You can't call a future strategy from a future technique. Nor would you be able to conjure a trigger that calls a future technique while running a future strategy. See the connection in the Resources for forestalling recursive future technique calls. 

The getContent() and getContentAsPDF() techniques can't be utilized in strategies with the future comment. 

You're restricted to 50 future calls for every Apex summon, and there's an extra breaking point on the quantity of brings in a 24-hour time frame. For more data on cutoff points, see the connection underneath.


Popular Salesforce Blogs