asynchronously(i.e in future when resources are available to it). Normal method runs as soon as
it executes in same thread but future method runs in different thread. Future method always require id's as parameter, Objects are not allowed as there is a possibility that when future method runs data might have changed which can lead to processing on wrong data.
When to use future methods?
- When you are having a callout to an external system.
- To avoid mixed dml error. (Mixed dml error occurs when you try to do dml operations on setup object(Ex: user role) and other objects in the same transaction).
Points to remember while using the future method:
1) The future method is always static with the void return type.
2) The future method always requires id's as parameter, Objects are not allowed.
3) Order of execution is not guaranteed with the future method.
4) When making a callout to external system future method is annotated with(callout=true).
5) Future method can’t invoke another future method.
Syntax of future method:
global class Myclass{
@future
public static void futureMethodName(list<object> id)
{
// Operations here
}
}
Test class for future method:
To test future method we need to call future method between test.startTest() and test.stopTest().
When test.startTest() runs data is collected asynchronously and as soon as a test.stopTest runs data collected is run synchronously.
test.startTest();
myClass.futureMethodName(id);
test.stopTest();
How to call a future method from Trigger?
Trigger:
Apex:
1) Why we cannot pass objects as arguments in future method?
Object data might change between the time you call the future method and the time it actually executes. and hence we pass record id.
2) If downtime occurs and future method was running what will happen?
The Future method execution will be rolled back and will restart after downtime overs.
3) If the future method was queued before a service maintenance what will happen?
It will remains in queue and when maintenance is over and resources are available it will get execute.
When to use future methods?
ReplyDelete.When you are having a callout to an external system....
Please Explain me this point sir...
when you make a service callout since we don't know how much time it will take to execute and moreover we don't want to stop our processing so we will use future method which will run assynchronous
DeleteFuture method are annoted with @future annotation which indicates that the method will run
ReplyDeleteasynchronously(i.e in future when resources are available to it).
>>Explain this one too..
hi its not only related to resource ...its to speedup the process ..like multiple job run same time paralel (asynchronous)
DeleteWhen test.startTest() runs data is collected asynchronously and as soon as a test.stopTest runs data collected is run synchronously.
ReplyDelete>>>What is exactly asynchronously & synchronously in this @future??
can we use future method inside batch apex class.
ReplyDeleteImportent point = you cannot call future method from another future method and also cannot call from batch.
ReplyDeleteThanks for possting this
ReplyDeleteHi thanks for sharingg this
ReplyDelete