You need to sign in to do that
Don't have an account?
Meggan Landis
In generating and attaching a pdf from an Opportunity, the fields are empty. Can anyone help?
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
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
It looks like our team of experts can help you resolve this ticket. We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!
https://jbshelpdesk.secure.force.com
Thanks,
Jarvis SFDC team
Try this code.
public class attachPDFToOpportunity2 {
private Id id;
//constructor
public attachPDFToOpportunity2(ApexPages.StandardController Controller) {
id = ApexPages.currentPage().getParameters().get('id');
system.debug('The Id is'+ id);
}
//method called from the Visualforce's action attribute
public PageReference attachPDF() {
//generate and attach the PDF document
PageReference pdfPage = Page.pdfOpp;
pdfPage.getParameters().put('id',id);
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 = id, Name = 'pdfAttachmentDemo.pdf', body = pdfBlob, ContentType = 'application/pdf');
insert attach; //insert the attachment
//redirect the user
PageReference pageWhereWeWantToGo = new PageReference('/' + id);
pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
return pageWhereWeWantToGo; //send the User on their way
}
}
I think this link may provide you with some useful information related to your issue https://help.salesforce.com/articleView?id=000339840&type=1&mode=1 (https://help.salesforce.com/articleView?id=000339840&type=1&mode=1)
Regards,
Surya