• Sebastian Becerra
  • 0 Points
  • Member since 2019

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
  • 2

I am using a custom controller to create the pdf from a visualforce, attach it to the record, and send it as an email attachment to an Opportunity Contact. I then use another Visualforce as a button to execute these actions.

The PDF file is created and sent correctly, but the merged fields used on the PDF Visualforce are not showing up. What could I be doing wrong? 

Visualforce PDF

<apex:page extensions="CertificateOfCompletionController" standardController="Opportunity" renderAs="PDF">
    <div style ="text-align:center;">
    <apex:image url="{!$Resource.TIG_Logo}" height="100" width="200"/>         
        <h1><br/><apex:outputText value="Certificate of completion"/></h1>
        <h2><br/><apex:outputText value="{!Opportunity.Contact_Name__c}"/></h2>
        <p/><br/><apex:outputText value="The intern group is delighted to confirm your completion 
                              of an internship program in {!Opportunity.Destination__r.Name}
                              from {!Opportunity.Start_Date__c} to {!Opportunity.End_Date__c} ({!Opportunity.Duration_weeks__c} weeks)" />       
        <p><br/><apex:outputText value="Role: {!Opportunity.Intern_Role__r.Company_Role__r.Name}" />
           <br/> <apex:outputText value="Hosting Company: {!Opportunity.Intern_Role__r.Placement_Company__r.Name}" /> 
    <div style ="text-align:justified;">
        <p><br/><apex:outputText value="The Intern Group aims to achieve three principal benefits for our program participants:" />
           <br/><ol style='1'>
        <li><apex:outputText Value="Deliver tangible, accelerated professional development through workplace exposure and training" /></li>
		<li><apex:outputText Value="Promote intercultural understanding" /></li>   
		<li><apex:outputText Value="Enable participants to gain invaluable experience in their chosen industry, in an exciting destination" /></li>           

<div  style="text-align:center;" >

<apex:outputText style="font-size:14px;font-family:Arial Unicode MS;" value="Issued on {!Opportunity.End_Date__c}" /><br/>
<apex:outputText style="font-size:12px;font-family:Arial Unicode MS;"  value="alumni@theinterngroup.com" /><br/>
<apex:outputText style="font-size:12px;font-family:Arial Unicode MS;"  value=" Phone:  UK +44 20 7193 4188				USA +1 718 878 6393				AUS +61 2 8011 3305;"/><br/>
<apex:outputText style="font-size:12px;font-family:Arial Unicode MS;" value="The Intern Group - UK reg. no. 07891350 - 41 Corsham St, Hoxton, London N1 6DR - UK" />



Custom Controller

public class CertificateOfCompletionController {
public final Opportunity a; //Opportunity object
	 String OppName ;
	public CertificateOfCompletionController(ApexPages.StandardController standardPageController) {
		a = (Opportunity)standardPageController.getRecord();//instantiate the Opportunity object for the current record
	//method called from the Visualforce's action attribute
	public PageReference CertificateActions() {

		//generate and attach the PDF document
		PageReference pdfPage = Page.CertificateofCompletion; //create a page reference to Visualforce page
		Blob pdfBlob; //create a blob for the PDF content
		if (!Test.isRunningTest()) { //if we are not in testing context
			pdfBlob = pdfPage.getContent(); //generate the pdf blob
		} else { //otherwise, we are in testing context and getContent() gets funky so create the blob manually
			pdfBlob = Blob.valueOf('Some Text for a boring PDF file...');
		Attachment attach = new Attachment(parentId = a.Id, Name = 'pdfAttachmentDemo.pdf', body = pdfBlob); //create the attachment object
        Id attachId = attach.parentId;
        List<Opportunity> CurrentOpp = new List<Opportunity>();
        CurrentOpp=[SELECT Id, Name, Contact_Email__c, Contact_Name__c From Opportunity WHERE Id=: attachId LIMIT 1];
		insert attach; //insert the attachment
  // Define fields neded from Opportunity
      //  string ContactEmail = CurrentOpp[0].Contact_Email__c;
 // Define the email
 Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

 // Create the email attachment    
 Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
 efa.setFileName('Certificate of completion.pdf');

  // Set the paramaters to the email object
  String subject = 'Certificate of completion for '+CurrentOpp[0].Contact_Name__c;
 email.setSubject( subject );
 // Here I am accessing current user email id to whom we are sending email
String body = 'This is testing for Send emails with attachment from your apex class in Salesforce'; 
   email.setToAddresses( new String[]{CurrentOpp[0].Contact_Email__c} );
 email.setPlainTextBody( body );
 // Attach file using setFileAttachments function
 email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

 // Sends the email
 Messaging.SendEmailResult [] r = 
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); 
		//redirect the user
		PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); //we want to redirect the User back to the Account detail page
		pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
		return pageWhereWeWantToGo; //send the User on their way

Action Visualforce:
<apex:page action="{!CertificateActions}" extensions="CertificateOfCompletionController" standardController="Opportunity">
		- Visualforce page for creating a PDF file, attach it to account and send it to the contact
	<apex:pageMessages ></apex:pageMessages><!-- included for display of errors should they occur -->
	<apex:detail inlineEdit="true" relatedList="true"></apex:detail> <!-- included so Account detail information is visible when errors occur -->

I am new to VF pages and controllers, any help would be very appreciated!​​​​​​​

I have a scheduler that runs every day @9am PST that sends out birthday emails.  However, when it ran this morning, it sent out emails to people who had birthdays yesterday.  I'm using Date.today() to grab the date, and the debug log shows that this returned yesterday's date.

09:00:01.0 (19039191)|VARIABLE_ASSIGNMENT|[10]|this|{"listDay":"0x638eaffe","monthDayCount":"0x38f524b0","startDate":"0001-12-30T00:00:00.000Z","todaysDate":"2017-02-09T00:00:00.000Z"}|0x3f7a5e67

Anyone have a clue why?  I checked my org's timezone settings as well as my user's settings, but everything is correctly set to 'Los Angeles'.  Am I missing something?

Hi i am having a custom button,which when clicked generates the PDF and saves it in attachment.
Now i am trying to make it as email too.
so when the custom button is clicked,generate pdf ,save the pdf to attachment and email it to particular user email.

How can i do it?Can i do it on single button?Or do i need two buttons. 1 .generate and save
2.email pdf?
I am confused and struck here .I am able to generate it and save as attachment.
But i want all three steps(generate,save and email) in single button.Please help.
Thanks in advance.