Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Multiple web service calls in a transaction
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 12:44 PM
Hi,
I have an Apex program that loops through database records, makes a web service call for each, and stores the result in the record. The web services uses a token which expires after 24 hours, so a record may make up to 3 web service calls -- 1 to fail, 1 to get the token, and 1 to succeed.
From my understanding SalesForce limits web service calls to 10 per database transaction so as a brute-force method, I am doing an update for each record like this:
// initialize wrapper, scope
AccountWrapper wrapper = new AccountWrapper();
List<Account> scope = [SELECT Id, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode FROM Account LIMIT 10];
// get districts
for (Account account: scope) {
wrapper.getDistrict(account, this.api);
update scope; // update scope to stay at or under Transaction Web Services governor limit
}
The Http web service requests are in wrapper.getDistrict. When I run this, I get a CalloutException:
recSystem.CalloutException You have uncommitted work pending. Please commit or rollback before calling out
My guess is that the update scope is not committing the transaction. How do I do a commit, or is there a better approach?
Solved! Go to Solution.
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:10 PM
You can't force the commit. The commit will automatically happen at the end of the apex code execution.
The issue is that you are using a DML command (in this cae update) in between service calls.
You need to issue your DML command (in this case update) out side of your loop. Program this like a trigger - NO DML calls in side of loops!
Salesforce.com Certified Advanced Administrator
http://www.forcedisturbances.com/
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:11 PM
Also, it is 10 http callouts per apex code execution. So if you need a max of 3 call outs per record, then you could only process 3 records which would give you a max of 9 call outs per execution and stay below the 10 callouts.
Salesforce.com Certified Advanced Administrator
http://www.forcedisturbances.com/
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:13 PM
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:17 PM
If you don't move your update outside of the loop, then you will not be allowed to call out again and you will get the uncommitted work error. You simply can't make a call out after a DML statement.
I think that the commit will happen once the apex code has stopped executing (i.e. a transaction) all together. It has nothing to do with a scope (of a function or variable). This is why you can't explicitly commit to the database.
Salesforce.com Certified Advanced Administrator
http://www.forcedisturbances.com/
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:17 PM
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:18 PM
My google geocoding engine for salesforce (link here) is a good working example of how to call out while having DML.
Salesforce.com Certified Advanced Administrator
http://www.forcedisturbances.com/
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:21 PM
Thanks, I'm reading your article now. Nice blog, good content and style!
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:27 PM
Thanks for the comments. Hopefully you can take that example, remove the geocoding part, and get it working. Then you can use some kind of filtering method (like I did with the date check) and schedule the batches to get the call outs going.
Salesforce.com Certified Advanced Administrator
http://www.forcedisturbances.com/
Re: Multiple web service calls in a transactio n
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-01-2012 02:32 PM
Excellent example, and very much like what I need.
I think the difference is that I can't cache my result -- the client runs my program when congressional districts change, a one-time event which essentially invalidates anything in my 'cache'. But what I could do is run my updater to mark all my districts as N/A or in need of resetting, and let the scheduled job find them.
Does that sound right to you?

