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
Lithium SFDCLithium SFDC 

Calling future method from Trigger which is invoked from a Batch

I have a use case as below:
1. On case insert/update we need to call an API.

Approach I have taken is: I wrote a future method to call API and called the future method in trigger.

Issue: Now our client wants to insert/update cases from batche job which results in error as future method can not be called from Batch.

Alternate I tried is queuable class but that again has a limitation of max 1 call from batch.
Any suggestion how to solve the above problem.
Nithesh NNithesh N
Is this Use Case stopping you from calling the API directly from batch Apex?  (By implimenting Database.AllowsCallouts)

For each iteration of execute() in the batch ie., For each "batch", we can call up to 10 HTTP callouts. 
So, Try to modify or Bulkify the API Call and modify the Batch Size accordingly. 

That may solve the issue. 

Let me know, if it works

Best,
Nithesh.
 
Lithium SFDCLithium SFDC
Thanks Nitesh, But we need to call API from trigger (Or any alternative to call API in realtime on case insert/update?). We do not intend to write any batch job, it's our client who has written batch for their needs and want to insert cases in batch. Which makes our trigger fail.
Nithesh NNithesh N
Try Wrapping up the API Call logic in
if (!System.isBatch()){
  //Call Api
​}
This will bypass the future call when it is called from Batch Call, and Trigger Won't fail in Normal Scenario. Your client needs to Implement different API Call logic. But This will atleast call the API in your trigger, as long as it isn't invoked from batch.

Is this Acceptable ??
 
Lithium SFDCLithium SFDC
Well, not an ideal solution but we have kept it as a last resort. Thanks. By the way, recently introduced Platform events can solve my problem.
Raja babu 12Raja babu 12
Hi @Lithium SFDC,
please let me know if you found any solutions.