Monday, December 19, 2022

What is Future Methods in salesforce

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 fromcontacts ) 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);
}

}
Instead of a Schedulable class, try using a batch class. batch classes can be scheduled using the UI, and can call @future methods.

global class ScheduledDispatcher Implements Schedulable{

public Interface IScheduleDispached{
void execute(SchedulableContext sc);
}

global void execute(SchedulableContext sc){
Type targetType = Type.forName(‘{HANDLERNAME’);
if(targetType != null){
IScheduleDispached obj = (IScheduleDispached)targetType.newInstance();
obj.execute(sc);
}
}

}

public class {HANDLERNAME} implements ScheduledDispatcher.IScheduleDispached {

public void execute(SchedulableContext sc)
{

//Call your Future Method Here

}

http://amitsalesforce.blogspot.sg/2015/02/future-methods-in-salesforce.html

—————————————————————————————————————————–

Explain about future method in salesforce?

Future method in salesforce: Future methods are used to run the process in a separate thread, at later time when system resources are available. We can use future methods for any operation we would like to run asynchronously in its own thread.

 

 

Future methods provide the benefits of not blocking the users from performing other operations and providing higher governor and execution limits for the processes.

Future method syntax:

 

 

  • Use @future annotation before method declaration.
  • Future methods must be static methods, and can only return a void type.
  • The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
  • Future methods can’t take standard or custom objects as arguments.
  • A common pattern is to pass the method a list of record IDs that you want to process asynchronously.

global class YourClassName {

  @future

  public static void yourFutureMethodName(List<Id> recordIds) {

    List<Account> acc = [Select Id, Name from Account Where Id IN :recordIds];

    // process account records to do awesome stuff

  }

}

Best practices to implement future methods:

 

 

  • Ensure that future method execute as fast as possible.
  • If using webservice callouts, try to bundle all callouts together from same future method, rather than using a separate future method for each callout.
  • Conduct thorough testing at scale. Test that a trigger enqueuing the @future calls is able to handle a trigger collection of 200 records. This helps determine if delays may occur given the design at current and future volumes.

 Consider using Batch Apex instead of future methods to process large number of records asynchronously. This is more efficient than creating a future request for each record.

To know more,  refer below Salesforce Trailhead URL.

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...