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
ChrisGountanisChrisGountanis 

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>
 
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');
  // 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;
 }
}
Ron HessRon Hess
you go into the setup area, click on setup, then develop, then Apex Class, then click the New button
paste in your code and save
ChrisGountanisChrisGountanis

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.

Ron HessRon Hess
you must build Apex code on a developer edition or sandbox

not on a production org
ChrisGountanisChrisGountanis
Got it thanks!