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
boratborat 

DML operations and callouts

I've run into a problem doing some DML operations and then making a callout, from reading other posts it seems this is "expected" behavior so if anyone has devised a workaround for this I would appreciate any feedback.

 

I've built a VF page that allows a user to save a lead, activity and campaign member record at once. Now I am adding some integration to an ordering system that makes a callout.

 

So this page will save a lead, activity and campaign member. These 3 record inserts are done with Database setpoints because sometimes I insert the lead and then the activity and an exception occurs so I need to roll the transaction back. This all works fine today. The problem that I am running into is that I want to make a callout after these 3 records have been inserted to an ordering system to pass over the Lead information. However, when I attempt to do this I get the following exception :

 

You have uncommitted work pending. Please commit or rollback before calling out

 

The error is rather funny in that salesforce actually doesn't permit you to do a commit. I've searched on the discussion boards and it's suggested to perform the callout first and then save the data afterwards. This doesn't work for 2 reasons, 1. I have to send the Lead Id as part of the transaction and 2. There are 50+ validation rules on the lead, so I have to save it first to ensure the data I am passing to the order system is valid in salesforce.

 

According to what I've read even if I performed a straight insert of these 3 records (and didn't add transactional control), I would still face this problem. It's also been suggested to perform the callout asynchronously, which also will not work for me since I need to wait for the response because the ordering system may reject my transaction and I need to let the end user know.

 

As a side note, I've built (and have it working properly) an ability to click a button from the Lead to send the Order data over a web service call. This of course will work since I am not inserting or updating the lead prior to making the web service call. If anyone has run into a problem like this and has some suggestions, I would appreciate hearing them.

 

Thanks.

gm_sfdc_powerdegm_sfdc_powerde

It's true that you cannot make callouts with pending transactions in the request context.  Also, it's not possible to do an explicit commit.  So your only way out is to make the callout in a separate context.   I understand that asynchronous call using @Future doesn't work for you. 

 

> It's also been suggested to perform the callout asynchronously, which also will not work for me since I need to wait for the response because the ordering system may reject my transaction and I need to let the end user know.

One work around that I can think of is to save the records and then respond to the user with a temporary page with a "Loading" message as you make the second call back to perform the callout.  To make it look seamless, you can also make successive  AJAX calls to save records and to make callout as user sees "Loading" message upon clicking "Save" button.  Hope this helps.