0

I have a scheduled job that executes a batch process that clones some opportunity records. When the records are inserted, it fires triggers on the opportunity that calls a future method, which throws the Future method cannot be called from a future or batch method. I could bypass the error by check if(!isBatch()){...

However, I need the trigger code to run and the method has to run async. What options are there to overcome the error and still have the future method run?

3

1 Answer 1

0

Queueable can be called from Database.Batchable classes. You must go through enqueueJob for this to work:

public class BatchClass implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
    return Database.getQueryLocator([SELECT Id FROM Account]);
}
public void execute(Database.BatchableContext context, Account[] records) {
    System.enqueueJob(new QueueClass(new Map<Id, Account>(records).keySet()));
}
public void finish(Database.BatchableContext context) {
}

}

public class QueueClass implements Queueable {
Set<Id> recordids;
public QueueClass(Set<Id> recordIds) {
    this.recordIds = recordIds;
}
public void execute(QueueableContext context) {
     // Do something here
}

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .