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
middha.vikram@gmail.commiddha.vikram@gmail.com 

Asynchronous web service call from apex

Hi,

 

I have a for loop in which I am calling a web service method of some other System. 

 

For(i=0;i<10;i++){

webserviceObject.method(parameter);

}

Problem here is that the call to webservice is asynchronous. Means before completing the first request, second request goes into that method and i get a error message from web service that "You have uncommitted work pending. Please commit or rollback before calling out" . I need to make sure that until first request is processed, second does not jumps in. Is there any way I can make this a synchronous process.

Thanks In Advance

Vikram

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

Your issue is not with the callout being aync. The web service callout is synchronous. The issue is that you must be making a DML statement (insert, update or delete) before making your 10 callouts. You can't make any DML statements before making an external callout. You can however make the DML statement(s) after the callout. With some code refactoring (i.e. moving your DML statement after the 10 web service callouts) you should be able to get this to work.

p.s. You're probably already aware of this, but you can only make 10 callouts in a single transaction. Your loop of 10 callouts should therefore be ok, but be careful not to exceed the governor limit.