function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luke Bray 6Luke Bray 6 

Is there any point using an @future method for DML operations?

I have inherited a lot of old code which uses @future methods for doing DML operations. Is there any benefit to doing this? The only thing I can think of is in case there are a lot of records to be updated but I thought @future methods were only really used for callouts.
 
Adheesh GuptaAdheesh Gupta
Hi Luke,

You are correct about the future methods being used for callouts, but they are also very useful for DML operations especially when you get caught in "Mixed DML Operation" error.

Mixed DML Operation error is thrown when you try to insert/update setup and non-setup objects in the same transaction. Hence, using @future method can help you out here, since the operations performed inside this method will be treated as a separate transaction which will be executed asynchronously.

This article will give you indepth knowledge about it:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm

Please mark this answer as solved if it answers your query.

Thanks,
Adheesh
chathura ranasinghe 10chathura ranasinghe 10
Hi Luke 

You can also use future methods to isolate DML operations on different SObject types to prevent the mixed DML error.

Refer below links :

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm#!
https://amitsalesforce.blogspot.com/2015/02/future-methods-in-salesforce.html

Thanks
Chathura
 
Dushyant srivastava 8Dushyant srivastava 8
Hi Luke,

What is Future Method:

A future method runs in the background, asynchronously. You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you'd like to run in its own thread, on its own time.

When to use Future Method:

If you want to make the execution of the apex program to run asynchronously then we make use of future method.When you specify
future , the method executes when Salesforce has available resources. For example, you can use the future annotation when making an asynchronous Web service callout to an external service.

Apex class with Future Method
public class AccountProcessor 
{
  @future
  public static void countContacts(Set<id> setId) 
  {
      List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
      for( Account acc : lstAccount )
      {
          List<Contact> lstCont = acc.contacts ;
          
          acc.Number_of_Contacts__c = lstCont.size();
      }
      update lstAccount;
  }
}

Test Class for the above:
@IsTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest() 
    {
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;

        Contact cont = New Contact();
        cont.FirstName ='Bob';
        cont.LastName ='Masters';
        cont.AccountId = a.Id;
        Insert cont;
        
        set<Id> setAccId = new Set<ID>();
        setAccId.add(a.id);
 
        Test.startTest();
            AccountProcessor.countContacts(setAccId);
        Test.stopTest();
        
        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
  }
  
}

For more Info please refer bellow Link:
http://amitsalesforce.blogspot.sg/2015/02/future-methods-in-salesforce.html

Best Regards,
Dushyant Srivastava