0

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

1 Answer 1

0

As the error says, you're trying to put an Account in to a List<Account>. This isn't legal in programming languages that have strict data types, such as Visualforce/Apex.

<apex:attribute name="account" description="" type="Account[]" />

Should be:

<apex:attribute name="account" description="" type="Account" />

With appropriate changes to your code.

Alternatively, you need to rewrite the PDF page to use an extension, and then create a list of Account records to use. For example:

<apex:page standardController="Account" renderAs="PDF" extensions="emailAccountController">
<apex:pageBlock title="Preview the Attachment">
    <c:attachment account="{!accountList}"/>
</apex:pageBlock>
</apex:page>

public class emailAccountController {
  public Account[] accountList { get; set; }
  public emailAccountController(ApexPages.StandardController controller) {
    accountList = new Account[] { (Account)controller.getRecord() };
  }
}

The reason why the first page works (sendEmail) is because you're using a List from a query.

1
  • Hi sfdcfox, After changing the attachment's component attribute to <apex:attribute name="account" description="" type="Account" /> and creating/adding an extension "emailAccountController" to pdf page . I got a error "Wrong type for attribute <c:attachment account="{!account}">. Expected Account, found Account[] for sendEMailPage and attachmentPDF Commented Sep 27, 2021 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .