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
Gopinath418Gopinath418 

what is the use of system.isfuture() ? Can any one explain with an example how to use it in @future methods or batch classes?

Best Answer chosen by Gopinath418
Ajay K DubediAjay K Dubedi
Hi 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.
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

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Gopinath,

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. Example: I hope it will be helpful.

please mark it as best answer if the information is informative.

BestRegards
RahulKumar
Ajay K DubediAjay K Dubedi
Hi Gopinath,

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
    } 
}
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
Gopinath418Gopinath418
Hi 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
Ajay K DubediAjay K Dubedi
Hi 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.
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
This was selected as the best answer
somasundaram P 5somasundaram P 5
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.

Regards,
Somasundaram.