0

We have created custom button based on visual force and apex code where i have using the vf email template. If we use plain text body then receiving the email but if we are using the template then receiving empty email.

VF page

<apex:page standardController="Contract_Standards__c" extensions="SendEmailPageController" action="{!send}">

</apex:page>

Apex controller

public class SendEmailPageController {

public String conId {get;set;}

public SendEmailPageController(ApexPages.StandardController controller) {

    conId=ApexPages.currentpage().getParameters().get('id');
    system.debug(conId);

}
public PageReference send()
{ 

EmailTemplate templateId = [Select id from EmailTemplate where name = 'AS CS Sales User Manual notification template'];  
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(templateId.id);
//String[] toAddresses = new String[] {'admin@chiper.com'};
//mail.setWhatId(conId);
mail.setTargetObjectId('00515000006c1Q0');
   // mail.setToAddresses(toAddresses);
      mail.setBccSender(false);
      mail.setSubject('Subject');
      mail.setSaveAsActivity(false); 
 // mail.setHtmlBody('Please find the attachment');
     // mail.setPlainTextBody('Test Message Body');'00Xq0000000QuVkEAK'
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
     return null;

    } 

 }
3
  • can you please share the email template as well.
    – Avijit Das
    Mar 23 '17 at 9:32
  • <messaging:emailTemplate subject="{!if(RelatedTo.Expedite_Review__c='Yes','Expedite','')} Opp Pre-Signature: Advance Notification-{!relatedTo.Opportunity__r.Name}" recipientType="User" relatedToType="Contract_Standards__c"> <messaging:htmlEmailBody >
    – Reddy
    Mar 23 '17 at 9:45
  • @Reddy You can click on the Edit link at the bottom left of a post any time you need to update or edit it.
    – crmprogdev
    Mar 23 '17 at 21:52
4

@Reddy, I fixed a similar issue few days ago. Remove

mail.setSubject('Subject');

from your code. I think once you provide the code with some part of mail(like subject, body) then it ignores the template.

4
  • That's perfect. Have you followed any guide or doc for doing this type of scenario. Pls do share if you have any.. I need to do more on this.
    – Reddy
    Mar 24 '17 at 3:01
  • One more info...Can you give some code for based on certain condition (if the users select the picklist value, say company is Airline) then mail need to be sent to email ids from one of the custom field on object.
    – Reddy
    Mar 24 '17 at 3:41
  • I follow apex dev guide and sometimes google for help and look at the API calls specifically.
    – SF Learner
    Mar 24 '17 at 10:26
  • 1
    @Reddy, All the merge fields must be present on the templates. From the code, you should set the templateID and setWhatId(WhatId) -- This will set the context for those merge fields on the template. setTargetObjectId(ctId) --This will set the context for the toaddress(email to which the mail has to be sent)Hope this helps.
    – SF Learner
    Mar 24 '17 at 10:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.