• Jeff Gilmore
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I have a triiger that, upon update or insert makes a number of callouts to an external API.  

So the trigger calls a method from a helper class that is defined as @future (callout=true) .

That all works great until I try to send an email as part of this sequence.  Then any callouts that occur after the email is generated give the error "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out".

I first tried sending this email through another method marked as @future, but was told that future methods can't call future methods.

So I changed it to a queable:
 
public class TribeSendEmail implements Queueable {
    
    String HTMLBody = '';  
    String recipientEmail = '';
    
    public TribeSendEmail(String HTMLBody, String recipientEmail) {  
  
        this.HTMLBody = HTMLBody;  
        this.recipientEmail = recipientEmail;  
    }  
  
    public void execute(QueueableContext qc) {  
        Messaging.reserveSingleEmailCapacity(1);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        String[] toAddresses = new String[] {recipientEmail}; 
        String[] ccAddresses = new String[] {foo@bar.org'};
        
        mail.setToAddresses(toAddresses);
        mail.setCcAddresses(ccAddresses);
        mail.setSubject('This is the subject');
        mail.setBccSender(false);        
        mail.setHtmlBody(HTMLBody);        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });    
    } 
}
Which I call as follows from the @future method:
System.enqueueJob( new TribeSendEmail( emailBody, recipient ) );
but it still gives me the errors on any subsequent callouts.

Note that there are no DML actions in any of these classes aside from the sending of the email, and if I comment out the "enqueJob" statement, it all works fine.

Can anyone suggest a way to send an email from within this context or advise if I am doing something wrong?

Thanks!



 
I have a triiger that, upon update or insert makes a number of callouts to an external API.  

So the trigger calls a method from a helper class that is defined as @future (callout=true) .

That all works great until I try to send an email as part of this sequence.  Then any callouts that occur after the email is generated give the error "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out".

I first tried sending this email through another method marked as @future, but was told that future methods can't call future methods.

So I changed it to a queable:
 
public class TribeSendEmail implements Queueable {
    
    String HTMLBody = '';  
    String recipientEmail = '';
    
    public TribeSendEmail(String HTMLBody, String recipientEmail) {  
  
        this.HTMLBody = HTMLBody;  
        this.recipientEmail = recipientEmail;  
    }  
  
    public void execute(QueueableContext qc) {  
        Messaging.reserveSingleEmailCapacity(1);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        String[] toAddresses = new String[] {recipientEmail}; 
        String[] ccAddresses = new String[] {foo@bar.org'};
        
        mail.setToAddresses(toAddresses);
        mail.setCcAddresses(ccAddresses);
        mail.setSubject('This is the subject');
        mail.setBccSender(false);        
        mail.setHtmlBody(HTMLBody);        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });    
    } 
}
Which I call as follows from the @future method:
System.enqueueJob( new TribeSendEmail( emailBody, recipient ) );
but it still gives me the errors on any subsequent callouts.

Note that there are no DML actions in any of these classes aside from the sending of the email, and if I comment out the "enqueJob" statement, it all works fine.

Can anyone suggest a way to send an email from within this context or advise if I am doing something wrong?

Thanks!