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
RajanRajan 

Email service with VF template

Hi friends,

​I have designed a custom button for sending email. I have a custom table where I'm displaying data. Whenever I'll select any record using checkbox and click that button then that particular record should go as email with that VF template which I'm using. I'm able to send email with that template but records are not coming. I think their is some problem with data mapping with that template. I'm displaying recods in table from 3 different custom objects with Account and contact standard object. Any idea or suggestion or sample code for this requirement?
Deepak Pandey 13Deepak Pandey 13
Hi Rajan 
Above requirement you have to learn your email template and replace with requirement .
Like- 
if(isOwnerChanged)
                    {
                        system.debug('objOpp....'+objOpp);
                        
                        //query on template object
                        EmailTemplate objET=[Select id,Subject, HtmlValue, Body from EmailTemplate where name=:'ChangedUserEmailNotification'];
                        
                        // process the merge fields
                        String subject = objET.Subject;
                        subject = subject.replace('{!Opportunity.Name}', objOpp.Name);

                        String htmlBody = objET.HtmlValue;
                        htmlBody = htmlBody.replace('{!Opportunity.Id}', objOpp.Id);
                        htmlBody = htmlBody.replace('{!Organization.Name}', Userinfo.getOrganizationName());

                        String plainBody = objET.Body;
                        plainBody = plainBody.replace('{!Organization.Name}', Userinfo.getOrganizationName());


                        //list of emails
                        List<Messaging.SingleEmailMessage> lstEmail = new List<Messaging.SingleEmailMessage>();
                        
                        //initiallize messaging method
                        Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
                        
                        singleMail.setTargetObjectId(objOpp.OwnerId);
                        
                        //set template Id
                        //singleMail.setTemplateId(objET.Id);
                        singleMail.setToAddresses(new String[] {objOpp.OwnerId});

                        //flag to false to stop inserting activity history
                        singleMail.setSaveAsActivity(false);
                        singleMail.setTreatTargetObjectAsRecipient(false);
                        
                        singleMail.setSubject(subject);
                        singleMail.setHtmlBody(htmlBody);
                        singleMail.setPlainTextBody(plainBody);

                        //add mail
                        lstEmail.add(singleMail);
                        system.debug('lstEmail....'+lstEmail);
                       
                       Messaging.sendEmail(lstEmail);

                    }
RajanRajan
Hi Deepak,

This will not work because I'm using VF email template.