+ Start a Discussion
Apex developer 21Apex developer 21 

Error: <apex:actionFunction> (under <c:emailcontroller>) must occur between <apex:form></apex:form> tags.

I get the error written in the title. When i add <apex:form></apex:form> tags it gives the error: Error: <messaging:emailTemplate> cannot contain <apex:form>.   So im stuck in a loop. Could someon help me out. Here is my VF email template, class and componet:
<messaging:emailtemplate recipienttype="contact" subject="Product Enquiry">
  <messaging:htmlemailbody > 
     Congratulations!
     This is your new Visualforce Email Template. Following are a list of randomly choosen accounts in my org:
    <!-- Embedding visualforce component here -->
    <c:emailcontroller >
  </c:emailcontroller></messaging:htmlemailbody> 
</messaging:emailtemplate>
 
<apex:component access="global" controller="SendemailController" >

<script type="text/javascript">
function init() {
sendEmail();
}
if(window.addEventListener)
window.addEventListener('load',init,true)
else
window.attachEvent('onload',init)
</script>
<apex:form>
<apex:actionFunction name="sendEmail" action="{!sendEmailFunction}">
</apex:actionFunction>
</apex:form>
</apex:component>
 
public class SendemailController {
public String AttachmentId {get;set;}

Public SendemailController(){
AttachmentId = ApexPages.currentPage().getParameters().get('Id');
system.debug('Attachment id->'+AttachmentId );
}

Public Pagereference sendEmailFunction(){
Attachment getEmail = [SELECT Id,Name,ContentType FROM Attachment where ParentId ='0Q00Y000000LIVL' LIMIT 1];
if(getEmail.Id != null) {
String toaddress = 'info@test.com';
system.debug(toaddress);

try {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {toaddress};
String[] ccAddresses = new String[] {'info@test.com'};
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);
mail.setReplyTo(toaddress);
mail.setSenderDisplayName('You have don it');
mail.setSubject('Testing email through apex');
mail.setBccSender(false);
mail.setUseSignature(true);
mail.setPlainTextBody('This is test email body. This mail is being sent from apex code');
//mail.setHtmlBody('<b> This is HTML body </b>' );

List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = '0Q00Y000000LIVL']){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
//mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
}
mail.setFileAttachments(fileAttachments);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

} catch(Exception e) {}
}

PageReference reference = new PageReference('https://eu11.salesforce.com/'+AttachmentId);
reference.setRedirect(true);
return reference;
}

}

 
Best Answer chosen by Apex developer 21
PrasathPrasath
 All email template tags must be wrapped inside a single emailTemplate component tag. emailTemplate must contain either an htmlEmailBody tag or a plainTextEmailBody tag. The detail and form components are not permitted as child nodes. This component can only be used within a Visualforce email template. Email templates can be created and managed through Setup | Communication Templates | Email Templates