+ Start a Discussion

Asynchronous web service call from apex



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





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


Best Answer chosen by Admin (Salesforce Developers) 

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.