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

How we can send Web Request after insert operation?

HI Everyone


As  many time i call a http webrequest before any dml operation using @future callout method.which work fine 

But how we can  call a http webrequest after the dml operation.

As i tried to call a  @future callout method after a insert but i got a callout.exception.


my code is as under:


public PageReference SendMail()
Request__c clFR = new Request__c ();
Request__c oldFR = [SELECT Flight_Itinerary_Name__c,Email_Bcc__c FROM  Request__c WHERE Id = :fr.Id];

clFR.Flight_Itinerary_Name__c= oldFR.Flight_Itinerary_Name__c;
clFR.Email_Bcc__c =oldFR.Email_Bcc__c 
insert clFR;

 WebServiceFlightReqEmail.HttpResult(clFR); // consist of http request only



please give me some idea to do that. 


Thanks in advance.




There is no alternate to do that, you have to perform all DML after callout. Salesforce does not have a explicit Commit, if you try doing DML and then Callout, sice future callout is Asyncronus process so that your data resides in inconsistent state. To prevent that salesforce inforce that limit.

Anup JadhavAnup Jadhav

What is the exception thrown? 


AFAIK you cannot invoke webservice callouts immediately after a dml operation (even inside a future method). You can change your design to create an Apex scheduler which will run every couple of minutes, retreive the inserted objects and send an email using the webservice. You'd need a flag on the object to indicate whether an email was sent or not.




I ran into this problem before as well. Luckily it looks like this is a controller for a page.


You have to split your method into two parts, commitRecord() and sendEmailNotificaiton()

you have to call sendEmailNotification in the onComplete event of the commitRecord action.


for example:

<apex:actionFunction name="sendEmailNotification" action="{!sendEmailNotification}" />
<apex:commandButton action="{!commitRecord}" onComplete="sendEmailNotification()" />


This way the DML happens on a different execution than web request, but they'll both be triggered by a single button press and they'll happen sequentially.


This takes advantage of the fact that Salesforce renders the actionFunctions as javascript functions with the name you provide. You can call any of your actionFunctions from javascript.


Hope that helps.