Understanding @future Methods in Salesforce: Async Processing Made Simple

Understanding @future Methods in Salesforce: Async Processing Made Simple 

What is a @future Method? 

In Salesforce, a @future method is an asynchronous Apex method used to run processes in the background—after the current transaction has been committed. This is especially useful when you’re performing time-consuming tasks or callouts that shouldn’t delay a user’s experience. 

🔧 Syntax 

apex 

@future 

public static void methodName(DataType param) { 

    // logic 

} 

 

When Should You Use a @future Method? 

Use @future when you need to: 

  • Make a web service callout from a trigger or synchronous code 
  • Update or insert records asynchronously to avoid hitting DML limits 
  • Avoid long-running operations blocking user experience 

 

Key Points and Limitations 

🔹 Aspect  🚫 Limitation 
Parameters  Only primitive types, not objects 
Return Type  Must be void 
Callouts  Allowed only with @future(callout=true) 
Limits  Max 50 future calls per transaction 
Execution Order  Not guaranteed 

 

Simple Example 

Let’s say we want to send a welcome email to a user after a new contact is created—but we don’t want the creation process to wait for the email to be sent. 

🔸 Step 1: Define the Future Method 

apex 

public class ContactHelper { 

    @future 

    public static void sendWelcomeEmail(String email, String name) { 

        // Simulate sending email 

        System.debug(‘Sending welcome email to ‘ + name + ‘ at ‘ + email); 

        // Actual logic would involve using Messaging.sendEmail 

    } 

} 

🔸 Step 2: Call It from a Trigger 

apex 

trigger ContactTrigger on Contact (after insert) { 

    for (Contact c : Trigger.new) { 

        if (c.Email != null) { 

            ContactHelper.sendWelcomeEmail(c.Email, c.FirstName); 

        } 

    } 

} 

This ensures the email is sent after the contact is created, without delaying the insert process. 

Real-World Use Case 

Scenario: Financial App – Credit Score Update 

A bank uses Salesforce to manage loan applications. When a user submits a loan form, Salesforce must call an external credit score API. This call should not delay the user or timeout the transaction. 

Solution: 

Use a future method to handle the callout. 

🔹 Step 1: Future Method with Callout 

apex 

public class CreditScoreService { 

    @future(callout=true) 

    public static void fetchCreditScore(String applicantId) { 

        // HTTP callout logic 

        Http http = new Http(); 

        HttpRequest req = new HttpRequest(); 

        req.setEndpoint(‘https://api.example.com/creditscore?id=’ + applicantId); 

        req.setMethod(‘GET’); 

        HttpResponse res = http.send(req); 

        if (res.getStatusCode() == 200) { 

            // Update applicant record with score 

            Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody()); 

            Integer score = (Integer) responseMap.get(‘score’); 

 

            Applicant__c app = [SELECT Id FROM Applicant__c WHERE Id = :applicantId LIMIT 1]; 

            app.Credit_Score__c = score; 

            update app; 

        } 

    } 

} 

🔹 Step 2: Trigger or Flow to Call It 

apex 

trigger ApplicantTrigger on Applicant__c (after insert) { 

    for (Applicant__c app : Trigger.new) { 

        CreditScoreService.fetchCreditScore(app.Id); 

    } 

} 

Benefit: The user doesn’t wait for the callout. The score is updated in the background. 

 

When NOT to Use @future 

Avoid @future if: 

  • You need to chain multiple async jobs → use Queueable 
  • You need complex object parameters → use Queueable or Batch 
  • You must track job status → use Batch or Queueable with Job IDs 

 

Conclusion 

The @future method is a quick and powerful tool for async operations in Salesforce—but it’s best suited for simple, fire-and-forget tasks. For more advanced async jobs, consider Queueable or Batch Apex. 

 

Leave a Comment

Your email address will not be published. Required fields are marked *