I have created a controller, vf page, component to generate a list of accounts and to share with email.
Controller:
public class sendEmail {
public String EmailId { get; set; }
public List<Account> account;
public sendEmail() {
account = [SELECT id,Name,Industry from Account];
}
public List<Account> getAccount() {
return account;
}
public PageReference send() {
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Reference the attachment page and pass in the account ID
PageReference pdf = Page.attachmentPDF;
for(Integer i=0 ;i<account.size(); i++)
{
pdf.getParameters().put('id',(String)account[i].id);
}
pdf.setRedirect(true);
System.debug('Get content'+pdf.getContent());
// Take the PDF content
Blob b = pdf.getContent();
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('attachment.pdf');
efa.setBody(b);
// Sets the paramaters of the email
email.setSubject('Test pdf' );
email.setToAddresses( new string[] {EmailId} );
email.setPlainTextBody( 'Test pdf' );
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}
VF page:
<apex:page controller="sendEmail">
<apex:pageBlock title="Send an Email to Your Representatives">
<apex:dataTable value="{!account}" var="acc" border="1">
<apex:column >
<apex:facet name="header">Name</apex:facet>
{!acc.Name}
</apex:column>
<apex:column >
<apex:facet name="header">Industry</apex:facet>
{!acc.Industry}
</apex:column>
</apex:dataTable>
<apex:form ><br/><br/>
<apex:outputLabel value="EmailId" for="EmailId"/>: <br/>
<apex:inputText value="{!EmailId}" id="EmailId" maxlength="80"/>
<br/><br/>
<apex:commandButton value="Send Email" action="{!send}"/>
</apex:form>
</apex:pageBlock>
<apex:pageBlock title="Preview the Attachment">
<c:attachment account="{!account}"/>
</apex:pageBlock>
</apex:page>
Component:
<apex:component access="global">
<!-- ADD THIS ATTRIBUTE TAG TO PASS IN THE ACCOUNT OBJECT -->
<apex:attribute name="account" description="" type="Account[]" />
<apex:dataTable value="{!account}" var="acc" border="1">
<apex:column >
<apex:facet name="header">Name</apex:facet>
{!acc.Name}
</apex:column>
<apex:column >
<apex:facet name="header">Industry</apex:facet>
{!acc.Industry}
</apex:column>
</apex:dataTable>
</apex:component>
VF page:
<apex:page standardController="Account" renderAs="PDF">
<apex:pageBlock title="Preview the Attachment">
<c:attachment account="{!account}"/>
</apex:pageBlock>
</apex:page>
This is the error I get from the PDF vF page:
Wrong type for attribute <c:attachment account="{!account}">
. Expected Account[], found Account