0

In a commandbutton I have an action called save and in that method in have sendemail method with condition in this way...

 public void save(){
        // my code here
      if(member.name=='MyName'){
         sendEmail(emailcontentbody);
      }
}

In sendemail method I'm having singleemail messaging values...when I check it with condition it is throwing me an error

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

Error is in expression '{!save}'

Edit: Formatted code from comment below:


@future(callout=true)
  public static void sendEmail(string emailcontentbody,string Template,string useremail,string billingemail){

  Messaging.singleEmailMessage email1 = new Messaging.singleEmailMessage();
  List emailcontentinfo = 
    [Select 
       Mail_Content_Data__c 
     From 
       Designer_Content_Data__c 
     Where 
       CnP_Designer__r.name =:Template ]; 

  for(Designer_Content_Data__c econtent : emailcontentinfo){
    emailcontentbody+=econtent.Mail_Content_Data__c; 
  }
  email1.seToaddresses('emailaddress');
  email1.setHtmlBody(emailcontentbody);
  Messaging.sendEmailResult[] res = Messaging.sendEmail(new Messaging.singleEmailMessage[]{email1});
}
6
  • There isn't an http callout in the picture, is there? Nov 1, 2012 at 10:29
  • It would be helpful to see the code of the sendEmail method. Clearly there is a Callout being performed either in or as a result of the sendEmail method, and it is throwing an exception, so it is not easy to diagnose this error without being able to see the callout code.
    – zachelrath
    Nov 1, 2012 at 10:35
  • no http callout in the picture
    – Eagerin Sf
    Nov 1, 2012 at 10:36
  • Dont believe this is causing your uncommitted exception, but make sure you're specifying the correct fields. For instance, I don't see any toAddresses specified. More info: salesforce.com/us/developer/docs/apexcode/Content/… Nov 2, 2012 at 14:00
  • settoaddress is their but forgot to add that in the code...
    – Eagerin Sf
    Nov 5, 2012 at 8:31

1 Answer 1

2

EDIT : Having said this, I've just tried sending an email after a DML Operation in both a Trigger and a Controller Save method, and it works okay in both cases. Are you doing something like saving inside a for loop and a send email after each save, all inside the for loop ?


Mixing callouts(I've only seen this behaviour with http callouts in the past) and dml operations in the same context is not permitted.

I'm guessing the reasoning behind this behaviour may be because salesforce hasn't fully committed the database operation, which can be rolled back, but any resulting action from the callout may not be idempotent. (I see it like a 2 phase commit in my mind)

One solution would be to move your sendEmail to a @future method.

(P.S. I'd also be tempted to try sending all emails first and then invoking the actually database save, to see if that also solves the problem.)

5
  • If I use @future(callout=true) then I'm getting Compile Error: Variable does not exist:although the variable exsist
    – Eagerin Sf
    Nov 1, 2012 at 9:59
  • Variables have to be declared static to be accessed in a @ future method, which has to be declared static. (you can only pass in primitives into a @ future method. It will not have access to all the variables in the current context) Nov 1, 2012 at 10:18
  • public void sendEmail(){Messaging.singleEmailMessage email1 = new Messaging.singleEmailMessage();list<CnP_PaaS__C_P_Designer_Content_Data__c> emailcontentinfo = [select Mail_Content_Data__c from Designer_Content_Data__c where CnP_Designer__r.name =:Template ]; for(Designer_Content_Data__c econtent:emailcontentinfo) {emailcontentbody+=econtent.Mail_Content_Data__c; }email1.setHtmlBody(emailcontentbody) Messaging.sendEmailResult[] res = Messaging.sendEmail(new Messaging.singleEmailMessage[]{email1});.....my code of email in save method is that....
    – Eagerin Sf
    Nov 1, 2012 at 10:53
  • How about post this code in your original question for more readibility. Might help to post the save an sendEmail methods. Nov 1, 2012 at 10:57
  • I formatted the code above, but can you please update it to include the save method as well? Looks like you are performing some additional logic in the save method that might throw the exception. Nov 2, 2012 at 13:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .