2

I have a requirement where I need to send the email template and also allow the use to change it and then send the content.

I am able to send email and attach a VF template dynamically. But I am not able to modify and then send the content of email template at same time.

  // Constructor to populate instance of your object
    public Cls_SendEmail(ApexPages.StandardController controller) {

        this.order = (Order__c )controller.getRecord();
        order = [SELECT name, ContactEmail__c , case__r.contact.email, Case__r.Case_Subject__C FROM Order__c
             WHERE id = :ApexPages.CurrentPage().getParameters().get('id')];
        to =  order.case__r.contact.email;

        emailTemplatebody = [Select id, subject, body from EmailTemplate where 
                                            DeveloperName=:'ET_EmailInvoice'];

        subject = emailTemplatebody.subject; 
        body =  emailTemplatebody.body; 
        order.EmailBodyFromAgent__c = body;                          
    }

    public pageReference send(){

            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();  
            String[] toaddress = new String[]{};    
            toaddress.add(to);
            email.setToAddresses(toaddress); //Use SOQL to retrieve addresses in the address            
            email.setBccSender(true);

            If ( cc != null )
            {
                String[] ccaddress = new String[]{};    
                ccaddress.add(cc);
              //  email.setCcAddresses(ccaddress);
            }

            email.setTargetObjectId(order.case__r.contact.Id);
            email.setWhatId(order.Id); 
            emailTemplatebody.body = body;

         // update emailTemplatebody;
            email.setTemplateId(emailTemplatebody.id);
            email.setSaveAsActivity(true);
            Messaging.SendEmailResult [] res = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email});      
            for ( Messaging.sendEmailResult result : res ) {
                   if ( !res[0].isSuccess () ) {
                       System.debug ( result  );
                   }
                   else{
                       ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
                       ApexPages.addMessage(msg);
                   }
               }  
            return null;

        }
  • I can't take credit for this but I recall that this solution was brilliant salesforce.stackexchange.com/a/8745/2602 -- you force SFDC to render and then rollback - rendered value is then available for user presentation – cropredy Aug 8 '14 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.