0

I want to know if we can use batch jobs to call future methods (I want to insert users and that triggers a future method that assigns an Id to the user and manipulates a couple of fields) so I want to know if it is viable or not.

4
1

I have found a way to call a future method from a scheduled class but not batch class.It might help you : -

global class Scheduledbacthfuture 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 Scheduledbacthfuture.IScheduleDispached {

  public void execute(SchedulableContext sc)
    {
        //Call your Future Method Here

    } 

}
3
  • @Ishan, you can up-vote also, so that other can find it useful.
    – Avijit Das
    May 2 '17 at 10:55
  • I am now getting an error which says FATAL_ERROR System.CalloutException: Callout from scheduled Apex not supported.
    – Ishan
    May 2 '17 at 12:04
  • SO it works like, From Batch, you call scheduler and from scheduler, you call future. Its going one way inside the stack. Nov 20 '17 at 9:50
1

You can not call future methods from batches. Actually batch and future both work in Async mode. For this case write one method which will check the request is coming from batch or not, if it is coming from a batch then call non-future method else call the future method.

For this purpose you can use System Class methods. eg.

If(System.isBatch()){
//call non-future method
}
else{
//call future method
}
6
  • Hi, @frodo I need to call the future method as it helps us fetch some important data without which user cannot do anything useful.
    – Ishan
    Apr 25 '17 at 5:55
  • @Ishan Actually i'm just saying that the you would need to organize your code a little bit. The code which you have write in the future method needs to be written in non-future method, then on the basis of request type you can call this method from a future method or if it is coming from a batch then you can call it directly.
    – Mr.Frodo
    Apr 25 '17 at 5:58
  • I don't think there is a way to implement callout without future method.
    – Ishan
    Apr 25 '17 at 6:00
  • @ishan yes you can. If the request is coming from a batch, there is no need to write future method to make callout.
    – Mr.Frodo
    Apr 25 '17 at 6:02
  • 1
    @Ishan see this answer
    – Mr.Frodo
    May 2 '17 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.