Understanding Asynchronous Future Methods

Understanding Asynchronous Future Methods

Let's start with the basics: What exactly are asynchronous future methods? In the realm of Salesforce, these methods provide the capability to execute code asynchronously, operating independently in the background, distinct from your primary process. This can be a game-changing feature, particularly when dealing with tasks that demand significant time or resources.

#AsynchronousFutureMethods #BackgroundProcessing #EfficiencyBoost

The Benefits

So, why should you care about asynchronous future methods? Let's break down some key benefits:

  1. Improved User Experience: Your users won't experience delays in their interactions with your Salesforce app while heavy tasks are running in the background.
  2. Avoiding Governor Limits: Asynchronous methods have their own set of governor limits, separate from synchronous methods. This can help you sidestep issues like SOQL query limits and execution time constraints.
  3. Scalability: As your data and user base grow, asynchronous methods can handle the increased load without bogging down your system.


Scenario: Order Processing Application

Description: Imagine you're developing a custom order processing application in Salesforce for a company that sells products. The application should create orders, manage inventory, and update related product quantities asynchronously when orders are placed. To achieve this efficiently, you'll use asynchronous future methods.

Code :

public class OrderProcessor {

    @Future
    public static void create_Order(String orderName,Map<Id, Integer> orderItems)
    {
        Order__c order = new Order__c(Name=orderName);
        insert order;
        
        for (Id productId : orderItems.keySet()) 
        {
          Integer quantitySold = orderItems.get(productId);
            updateInventoryAsync(productId, quantitySold);  
        }
       
    }
    
    
    public static void updateInventoryAsync(Id productId, Integer quantitySold)
    {
        product__c product = [SELECT Id,Quantity__c FROM product__c WHERE Id =: productId FOR UPDATE];
        product.Quantity__c -= quantitySold;
        update product;
    }
}        

Test Class :

@IsTest
private class OrderProcessorTest {

    @isTest
    private static void createOrderTest()
    {
        product__c testproduct = new product__c(Name='Test Product',Quantity__c=100,Price__c=50);
        insert testproduct;
         Order__c testorder = new Order__c(Name='Test Order');
        Map<Id,Integer> testOrderItem = new Map<Id,Integer>
            {
                testproduct.Id =>5
            };
        
        Test.startTest();
        OrderProcessor.create_Order(testorder.Name,testOrderItem);
        Test.stopTest();
        
        List<Order__c> updatedOrder = [SELECT Name FROM Order__c WHERE Name =: testorder.Name];
        List<product__c> updatedProduct = [SELECT Quantity__c FROM product__c WHERE Id =: testproduct.Id];
        
        System.assertEquals(1, updatedOrder.size());
        System.assertEquals(95,updatedProduct[0].Quantity__c);
    }
}        

Thank You !!

To view or add a comment, sign in

Explore content categories