As we already discussed that 'System.isfuture()' method will return 'TRUE' if the currently executing code is invoked by code contained future annotation (For eg. @future ) ,otherwise returns "FALSE'
Here is the small piece of code to show you the example,I will suggest you to run it once and see the output.
public class AccountProcessor
{
@future // future annotation
public static void CheckMethod(){
Boolean b=System.isFuture(); // fuction 'isfuture()' to the '@future' annotation
System.debug('>>>>>isFuture value is ='+b);
// b='TRUE' and if you removed the '@future' from the code it returns b='FALSE'
}
}
Please mark my answer as a solution if it is helpful Regards, Ajay
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 a 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.
isFuture() Returns true if the currently executing code is invoked by code contained in a method annotated with future; false otherwise.
Signature public static Boolean isFuture()
Return Value Type: Boolean
Usage Since a future method can't be invoked from another future method, use this method to check if the current code is executing within the context of a future method before you invoke a future method.
System.isfuture() method will return true if the currently executing code is invoked by code contained in a method annotated with future; false otherwise.
Usage: Since a future method can't be invoked from another future method, use this method to check if the current code is executing within the context of a future method before you invoke a future method.
Apex Class:
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;
}
}
Apex Batch
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
}
}
Where could we use system.isfuture() method in the example given by you.As i didn't see the usage of system.isfuture() in the example. Could you please explain it with one example if possible?
As we already discussed that 'System.isfuture()' method will return 'TRUE' if the currently executing code is invoked by code contained future annotation (For eg. @future ) ,otherwise returns "FALSE'
Here is the small piece of code to show you the example,I will suggest you to run it once and see the output.
public class AccountProcessor
{
@future // future annotation
public static void CheckMethod(){
Boolean b=System.isFuture(); // fuction 'isfuture()' to the '@future' annotation
System.debug('>>>>>isFuture value is ='+b);
// b='TRUE' and if you removed the '@future' from the code it returns b='FALSE'
}
}
Please mark my answer as a solution if it is helpful Regards, Ajay
Hi Gopinath418, Before calling future method you have to use the condition like below if(System.isFuture() == false){ futureClassName.futureClassMethod(); } hope it will sort your problem.if its works kindly mark as a solution.
As we already discussed that 'System.isfuture()' method will return 'TRUE' if the currently executing code is invoked by code contained future annotation (For eg. @future ) ,otherwise returns "FALSE'
Here is the small piece of code to show you the example,I will suggest you to run it once and see the output. Please mark my answer as a solution if it is helpful
Regards,
Ajay
All Answers
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 a 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.
isFuture()
Returns true if the currently executing code is invoked by code contained in a method annotated with future; false otherwise.
Signature
public static Boolean isFuture()
Return Value
Type: Boolean
Usage
Since a future method can't be invoked from another future method, use this method to check if the current code is executing within the context of a future method before you invoke a future method.
Please refer the below link for reference.
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm
Example:- http://amitsalesforce.blogspot.sg/2015/02/future-methods-in-salesforce.html
I hope it will be helpful.please mark it as best answer if the information is informative.
BestRegards
RahulKumar
System.isfuture() method will return true if the currently executing code is invoked by code contained in a method annotated with future; false otherwise.
Usage:
Since a future method can't be invoked from another future method, use this method to check if the current code is executing within the context of a future method before you invoke a future method.
Apex Class: Apex Batch
Instead of a Schedulable class, try using a batch class. batch classes can be scheduled using the UI, and can call @future methods. To understand more follow the link :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm
I hope, It will help you.
Please mark my answer as a solution.
Regards,
Ajay
Where could we use system.isfuture() method in the example given by you.As i didn't see the usage of system.isfuture() in the example. Could you please explain it with one example if possible?
Regards,
Gopinath
As we already discussed that 'System.isfuture()' method will return 'TRUE' if the currently executing code is invoked by code contained future annotation (For eg. @future ) ,otherwise returns "FALSE'
Here is the small piece of code to show you the example,I will suggest you to run it once and see the output. Please mark my answer as a solution if it is helpful
Regards,
Ajay
Before calling future method you have to use the condition like below
if(System.isFuture() == false){
futureClassName.futureClassMethod();
}
hope it will sort your problem.if its works kindly mark as a solution.
Regards,
Somasundaram.