You need to sign in to do that
Don't have an account?
ChrisGountanis
Emailing PDF Attachments using Blog Example
Trying to sample this code:
<apex:page renderAs="{!if($CurrentPage.parameters.p==null,null,'pdf')}" controller="MyController">
<apex:pageBlock title="MyDual-RenderingInvoice">
<apex:pageBlockSection title="Section1">
Text Section 1
</apex:pageBlockSection>
<apex:pageBlockSection title="Section2">
Text Section 2
</apex:pageBlockSection>
</apex:pageBlock>
<apex:form>
<apex:commandLink rendered="{!$CurrentPage.parameters.p==null}"value="PDF"action="{!deliverAsPDF}">
</apex:commandLink>
</apex:form>
</apex:page>
<apex:pageBlock title="MyDual-RenderingInvoice">
<apex:pageBlockSection title="Section1">
Text Section 1
</apex:pageBlockSection>
<apex:pageBlockSection title="Section2">
Text Section 2
</apex:pageBlockSection>
</apex:pageBlock>
<apex:form>
<apex:commandLink rendered="{!$CurrentPage.parameters.p==null}"value="PDF"action="{!deliverAsPDF}">
</apex:commandLink>
</apex:form>
</apex:page>
It says MyController does not exist and it does not give option to create. How do I create class or compenent, whatever it wants... manually?
This would be the code for the custom controller.
public class MyController {
public PageReference getDeliverAsPDF() {
// Reference the page, pass in a parameter to force PDF
PageReference pdf = Page.foo;
pdf.getParameters().put('p','p');
pdf.setRedirect(true);
// Grab it!
Blob b = pdf.getContent();
// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('From getDeliverAsPDF!');
String [] toAddresses = new String[] {'foobar@youremailaddressz.com'};
email.setToAddresses(toAddresses);
email.setPlainTextBody('Here is the body of the email');
public PageReference getDeliverAsPDF() {
// Reference the page, pass in a parameter to force PDF
PageReference pdf = Page.foo;
pdf.getParameters().put('p','p');
pdf.setRedirect(true);
// Grab it!
Blob b = pdf.getContent();
// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('From getDeliverAsPDF!');
String [] toAddresses = new String[] {'foobar@youremailaddressz.com'};
email.setToAddresses(toAddresses);
email.setPlainTextBody('Here is the body of the email');
// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
efa.setBody(b); //attach the PDF
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// send it, ignoring any errors (bad!)
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('MyPDF.pdf'); // neat - set name of PDF
efa.setBody(b); //attach the PDF
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
// send it, ignoring any errors (bad!)
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}
paste in your code and save
That is what I assumed as well. The only button I have available is "Run All Tests". IS this a user rights issue maybe? I have developer mode enabled and Visualforce pages are being created OK to start.
not on a production org