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
Avinash JanardhananAvinash Janardhanan 

Callout within a transaction

Hi,
I have a requirement, where I need to create a record in Salesforce. Get the ID of the record which got created and then make a webservice call passing this id as one of the parameter. If the webservice call fails, I need to rollback the whole transaction. Is this possible to do?

I tried using Savepoint, but I get the error you have uncommited transactions when I do the callout. Is there a workaround? 
Some forums suggested using @future notations, but I need to rollback my DML operation if the callout fails, so can't make it async. 

Any suggestions will be appreciated. 
 
Deepak BalurDeepak Balur
I don't know the requirement background but why would something be deleted that a user has enetered but could not be synched or passed to another system. Look at options toReTry using @future.
Anyways, Do a soft delete first such that these records don't show up in your views/reports and then Delete records using options like MassDelete.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Avinash Janardhanan:

  Looks like you are calling out with out a future context, in other words you are performing this  from a controller of a page correct?
 If you are using it from a controller, you can make use of onComplete property of command button or commandlink or actionfunction tag from which the method is invoked

For eg.,
Page:
<apex:commandButton action="{!someaction of yours}" value="Verify" onComplete="sendCallout();" />
<apex:actionFunction name="sendcallout" action="{!sendactualcallout}" />

Controller:

public static pagereference someactionofyours(){
//capture a savepoint before saving the record in a static variable context
//insert record here and save the id in a variable that has scope for entire class

}


public pageReference sendactualcallout(){
//send you callout, based on result, rollback or leave it
}

If it is not a too complex other than pure insert and delete operation, i would suggest you to insert a record once the transaction is complete because you are deleting the record anyway on failed transaction, you can use save point if there is complex functionality around the object in which you are trying to insert a record(multiple triggers that modified other records data)! 

Hope it helps.,

Thanks,
Balaji

 
Avinash JanardhananAvinash Janardhanan
Thanks Deepak and Balaji.. the basic requirement is to have a unique reference for the record created in salesforce in the external system as well, so that we have a way to trace back where the record originated from. So thats why I am trying to pass the record id created in salesforce to the external system using the web callout. For my purpose if there is any way of creating a unique reference in salesforce using apex code, i can very well use that unique reference, do the web callout and on success, save the same in salesforce. Then there will not be any need for doing the DML before the callout.

I will try out your suggestion Balaji.