• Niranj
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi all-
I'm taking my first stab at visualforce pages. I am trying to generate a pdf of an opportunity record and having it attach to the record via a custom button. I feel like i'm close because I can see the pdf working when I just load the VF page (with fields populated), and then the button I created is "generating" an attachment on the record, but I'm missing something in the middle becuase the pdf attachment fields are empty. I've pieced together code samples I found online.

The first thing I did was create a VF page, pdfOpp:

<apex:page standardController="Opportunity" renderAs="pdf">
<center>
<h1>PRE-ADMISSION PHONE SCREEN</h1>
</center>

<table>
<b>DETAILS:</b>
<hr/>
<tr><th>Pre-Admission Phone Screen Name:</th>
    <td><apex:outputText value="{!Opportunity.Name}"/></td>
    </tr>
<tr><th>Owner:</th>
    <td><apex:outputText value="{!Opportunity.Owner.Name}"/></td>
    </tr>
<tr><th>Created Date:</th>
    <td><apex:outputText value="{0,date,long}">
        <apex:param value="{!Opportunity.CreatedDate}"/>
        </apex:outputText></td>
    </tr>
 <tr><th>Account Name:</th>
    <td><apex:outputText value="{!Opportunity.Account.Name}"/></td>
    </tr>
 <tr><th>Stage:</th>
    <td><apex:outputText value="{!Opportunity.StageName}"/></td>
    </tr>
</table>
</apex:page>



I then created an apex class:

*/
public class attachPDFToOpportunity2 {
    
    private final Opportunity a; //Opportunity object
    
    //constructor
    public attachPDFToOpportunity2(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 attachPDF() {
        
        //generate and attach the PDF document
        PageReference pdfPage = Page.pdfOpp; //create a page reference to our pdfOpp 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() 
            pdfBlob = Blob.valueOf('Some Text');
        }
        Attachment attach = new Attachment(parentId = a.Id, Name = 'pdfAttachmentDemo.pdf', body = pdfBlob); //create the attachment object
        insert attach; //insert the attachment
        
        //redirect the user
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); //we want to redirect the User back to the Opportunity detail page
        pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
        return pageWhereWeWantToGo; //send the User on their way
    }
}

I next created the second VF page called attachPDFtoOpportunity:

<apex:page action="{!attachPDF}" extensions="attachPDFToOpportunity2" standardController="Opportunity">

    <apex:pageMessages ></apex:pageMessages><!-- included for display of errors should they occur -->
    <apex:detail inlineEdit="true" relatedList="true"></apex:detail> <!-- included so Opportunity detail information is visible when errors occur -->
</apex:page>

Lastly, I created a custom button. When I click on my Generate PDF button, it does generate a pdf and saves it to my Opportunity record, but it only includes headers and field names. No fields are actually populated.

Thank you!!!
Megg
Hi All, 

Im trying to do the second challenge in App Customization  but am getting some Super badge am getting the error "Challenge Not yet complete... here's what's wrong:  Couldn’t find the 'Volunteer Shift' relationship field or it is not configured correctly."
Please, anyone, can anyone tell me which relationships should be created between these objects? I have created a master-detail relationship between them. 
User-added image

Also, am not clear with this "Shifts with cascading delete and the option of roll-up summary fields." 

Thank you and any help is appreciated.
Thank you in advance.