I have a callout class to make a call to a web service, I would like to send an email when the callout fails:
String ret=''; try { ret = ApprovalService.CreateApprovalRequest(sessionId, obj.Name, obj.Description__c); } catch(System.CalloutException e) { ret = e.getMessage(); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'[email protected]'}; mail.setToAddresses(toAddresses); mail.setSubject('Exception occurred when calling Metastorm interface for ' + obj.Name); mail.setPlainTextBody(e.getMessage()); mail.setHtmlBody(e.getMessage()); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } ApprovalService.Logout(sessionId);
When I call this code and the callout fails I get an error:
You have uncommitted work pending. Please commit or rollback before calling out
for the
ApprovalService.Logout(sessionId);line.
But if I comment this line
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
I don't get the error, of course the email is also not sent also so I'm back to square one.
I don't understand why the sendEmail should request a commit or rollback, it's not supposed to be a DML operation