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
behappybehappy 

Sending Visualforce page by email

Hi,

I'm brand new in the world of visualforce. I made a VF-page on a custom object, rendered as pdf. This works. Now I'd like to have the possibility to send this VF-page easily to a contact person as an attachment. Can someone help me please ? What do I have to add to the VF-page itseld, what to 'components', what to 'classes' etc... ?

Many thanks in advance.

 

@anilbathula@@anilbathula@

Hi beHappy,

 

Just u need to write a email attachment method and send email method in the class 

In the visualforce page create a button send email call the email method in that action.

Then u can send ur vf page as email.

http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_sending_attachments.htm

Just check the above link for more reference.

 

Thanks

Anil.B

behappybehappy

Many thanks for this quick reply. I'll try it out and I'll give you feedback.

behappybehappy

Hi Anilbathula,

I'm trying out what's explained in the hyperlink you sent me but am facing some problems. Like I told, It's all brand new for me and I've nearly no experience at all.  Am I right that I must have the following ?

-VF page named attachmentpdf

- VF page named sendmail

- VF component named attachment (error : unknown property 'account')

- Apex class named sendEmail (any help for the testclass ?)

It would be great if you could set me on the right way.

Many thanks in advance.

Jean

 

APathakAPathak

Hi here is a simplified version :- 

 

Since your PDF visual force page is working you need to create one more visual force page and its associated controller:- 

 

VF Page:- 

<apex:page controller="sendEmail">
    <apex:pageBlock title="Send an Email to Contact">
        <apex:form><br/><br/>
            <apex:commandButton value="Send Email" action="{!send}"/> 
        </apex:form>
    </apex:pageBlock>
</apex:page>

 Controller:- 

 

public class sendEmail {
    public String subject{get;set;}
    public String body{get;set;}
	public List <String> ToAddresses{get;set;}
    
    public sendEmail() 
	{
		ToAddresses = new List <String>();
		ToAddresses.add('contactEmail@test.au'); //Add your custom logic to extract email of contact  
    }

    public PageReference send() 
	{
        // Define the email 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        PageReference pdf =  Page.attachmentPDF;//Replace attachmentPDF with the page u have rendered as PDF
        pdf.getParameters().put('id',(String)account.id); 
        pdf.setRedirect(true);

        // Take the PDF content
        Blob b = pdf.getContent();

        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('Attachement');
        efa.setBody(b);

        // Sets the paramaters of the email 
        email.setSubject( subject );
        email.setToAddresses( ToAddresses );
        email.setPlainTextBody( body );

        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Sends the email 
    
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
		
        return null;
    }
}

 

behappybehappy

Hi,

Many thanks for your quick reply. I've following Compile Error : Incompatible types since an instance of Schema.SObjectField is never an instance of String at line 17 column 38. This is the following line :

pdf.getParameters().put ('id', (String) account.id);

Can you also suggest me a testclass please ?

Sorry for all these questions, but I've a lot to learn and I'm sure I'm here by the right people to do that.

bye

Behappy

APathakAPathak

Try Replacing that line by - 

 

pdf.getParameters().put('id',account.id); 

 

As for Test Class the below is a great way to get started -

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

behappybehappy

Hello,

thanks for you reply. I tried your correction and now I've following error : Compile Error: Incompatible value type Schema.SObjectField for MAP<String,String> at line 17 column 29

I'll go to your hyperlink for the testclasses. thx