0

Trigger batch size can be 200. If i call future method(callout=true) on every batch object, this will throw limits error or not?

If it throw, how can i avoid this?

2
1

Salesforce has restriction that future methods cannot be called from Batch class.It will throw error "FATAL ERROR: Future method cant be called from future or Batch".

To avoid this Queueable Apex is the best option, it is a hybrid model of Future and Batch apex which supports getting called from batch apex and even from future.

You then have documentation that tells you that you are limited to initiating 50 Maximum number of Apex jobs added to the queue with System.enqueueJob in a given session during synchronous operation and 1 during aysnchronous operation.

1

As hinted by this article and as explained in this previous Q&A, a trigger for an SObject (rather than Platform Event) is invoked with up to 200 records at a time. If you have more than 200 records of the same object type in your DML insert/upsert/update then the trigger is called multiple times with different chunks of the DML records.

You then have documentation that tells you that you are limited to initiating 50 async operations (including futures and queueables) in a given session.

Because you are initiating a future from a trigger, each future does count towards that limit of 50. (Note that you can have a limit of 1 if the trigger itself is invoked from an async context, see later.)

Note how 50 (max futures) * 200 (max records per trigger call) = 10000 then realize that you are actually limited to DML on up to 10000 records in a given transaction (again as per the documentation).

So if you only perform a single DML operation to insert/update 10000 records that are processed by your trigger you won't actually exceed the async executions limit. Where you might is if you have multiple DML operations against smaller numbers of records which result in more than 50 executions of your trigger.

As Ranjit Mishra said in the other answer, Queueables are a bit more flexible (not least relating to their initiation context, but also in what types of data you can pass to them) so may be a better choice than a future.

Your trigger could conceivably be called from an already asynchronous context (such as a batch). However, in that scenario you are basically hosed if the trigger is called more than once because regardless of the type of async execution you choose to use (future or queueable) you can only invoke one new async process from an async execution context.

As such you should exercise extreme caution in choosing this approach; later changes in your application may mean you cannot do this.

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