+ Start a Discussion
code developercode developer 

Multiple callouts

Hi,

 

Is there any way to do multiple call outs in a single txn where there are DML operations?

Chris Gary CloudPerformerChris Gary CloudPerformer
You can do multiple callouts, as long as all of the callouts are done BEFORE any of the DML operations.  I know - that probably doesn't help you because you are most likely looking to perform a callout using the result of a DML operation.  Might I suggest in that case, looking at calling @future methods. The Operation could be framed as the following:
1. Perform callout #1 to obtain data.  
2. Insert callout results into a Salesforce record.
3. Call '@future' Method and pass the 'Id' of the record you just created.
4. in your '@future' annotated method, perform your second callout and the rest of your operations as needed.

'@future' methods can perform callouts as long as the method is documented with the '@future(callout=true)' annotation.  Please be warned, however, that this process only works for 2 callouts. Unfortunately, because you CANNOT call another @future method when you are executing inside of one, you cannot use this process to perform another callout after a DML once in your first @future method. Also, please note that technically calling '@future' methods spawns another transaction context, so in-memory variables and such will not be available to use in your '@future' method unless you can re-create them.
see https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_future.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_future.htm" target="_blank) for more information.
Suraj SinghSuraj Singh
If Object are added one by one then the trigger size will always be 1.
You could build a check into the trigger to e.g. block batch size larger than 1.
Alternatively, you could maximize the batch size to 10, since that is the max amount of callouts in 1 transaction. You could even make it 100 if you really want to, because you can start 10 @future contexts in 1 transaction, each of which can do 10 callouts.
Best practice, however, is to ask third party webservices to be batch-proof.
Anilkumar KotaAnilkumar Kota
To make multiple callouts to a long-running service simultaneously from a Visualforce page, you can add up to three requests to the Continuation instance. An example of when to make simultaneous callouts is when you’re making independent requests to a service, such as getting inventory statistics for two products.
When you’re making multiple callouts in the same continuation, the callout requests run in parallel and suspend the Visualforce request. Only after all callout responses are returned does the Visualforce process resume.
Ajay mishraAjay mishra
Hi there,

It is possible to do using Queueable Apex.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

This will help to call multiple callout in single transaction. 

But, Still want to mention it also have limit.

Please read the article carefully.

Thanks 
Ajay Mishra
Email: mishraajay.m1@gmail.com
Narasimha Reddy 69Narasimha Reddy 69
callout limits has changed in winter 15
Limit DescriptionOld ValueNew ValueMaximum number of methods with the future annotation allowed per Apex invocation10 to 50
Maximum number of callouts (HTTP requests or Web services calls) in a transaction10 to 100

for more info: https://releasenotes.docs.salesforce.com/en-us/winter15/release-notes/rn_apex_limits_callout_future.htm