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
Hermes GomezHermes Gomez 

Create a PDF from record and records on related object

Hi guys, 

I am trying to create a PDF file just like Quotes work. However im doing this from a Custom Object. 

I found this link that answers it in some way :

https://developer.salesforce.com/forums/?id=9060G000000I7qhQAC

However im looking to create a pdf with some info from the record that is being created and the Line Items on the quote will come from data on records which this main record is related to.

Better explaining, i have Object A which will have the button to create the PDF, some of the main data will be pulled from the record on Object A, the line items(which may be just one or many, based on a criteria that i also want to specify) will pull from Object B, which has a lookup field to Object A on every record on the org. 

So the PDF will generate with info from OBject A and every Object B that has object A related and meeting the criteria. 

 

Lastly i want a second button that will create the PDF but will attach the PDF to an email with a preselected templated(email template created on salesforce) and automatically send it to the email on a contact lookup field on Object A.

 

Please advise the best way to create this procedures. 

 

I really appreciate your help!

Dev_AryaDev_Arya
hi Hermes,

i can point you to another post where similar requirement was discussed, though the question is not closed but still points in the right direction
https://developer.salesforce.com/forums/?id=906F0000000D8wXIAS

in this post, venkat-d mentioned
"you can create a custom button on the parent object. Please follow below process .
1. Create a vf page with the format you  need for child records 
2. Create a cutom javascript button that calls a class method. 
3. In the class instantiate the vf page created in 1 and do getcontent()
4. Create a new attachment variable with body as the response from getcontent() 
5. Attach the attchment to parent record
6.send email using the same info to requried users."

if you need code help, then let me know, i will try to replicate it my dev org n post it here. 

cheers,
Dev

 
Hermes GomezHermes Gomez
Hey, thank you so much for your reply. I will definitely need some code help. The coding part of salesforce is my weak side lol.
Dev_AryaDev_Arya
I will then work on it over the weekend. in the mean time you could also try it. sure it will be fun. :-)
Hermes GomezHermes Gomez
I will definitely give it a try. This already is enough info for me to get to work. Thanks for your help in advance!
Hermes GomezHermes Gomez
Hello Dev,

I have made some progress on what to do.

I figured out on how to render my HTML as a PDF from my record on a custom object. 

However,

im stuck on how to pull the data from the record. keep in mind this is a custom object so im thinking i need to create a class and go from there.

Ive been trying to research on class development but im a bit stuck. 

Any help will be truly appreciated. 
Dev_AryaDev_Arya
Hi Hermes,

I am sorry, I could not work on it, over the weekened we got stuck with some deployment issues and then I got sick. 

I hope you have solved your problem so far, otherwise lets see what we have so far. I believe from your custom button, using JS you are calling Apex Class Function, where you get the record Id and can generate the pdf using PdfGenerator Template.

In the same function u can also get the record details. Below is the pseudo code, I just wrote it on the fly. You may need to edit it a lot.
public PageReference savePdf() {

//For pdf you can have ur record in html and Blob can convert it to pdf. This is one way, I am sure there are other smart ways of doing so.
//using SOQL in ur code, get the record details and add to the string as follows
String pdfContents = '<html><body>';
pdfContents += '<div>';
// Loop start
pdfContents += 'here you start running a loop and get the list details.<br/>' + ;
//loop ends
pdfContents += '</div>';
pdfContents += '<div>';
pdfContents += 'some extra text.<br/>';
pdfContents += '</div>';
pdfContents += '<div>';
pdfContents += 'Regards<br/>';
pdfContents += UserInfo.getUserName();
pdfContents += '</div>';
pdfContents += '</body>';
pdfContents += '</html>';

Blob pdfBlob = Blob.toPDF(pdfContents);
Blob pdfBlob = Blob.toPDF(contents);

// once in blob u can send it as an email
}

I hope this helps. And appologies again for the delayed response.

Cheers,
Dev
Hermes GomezHermes Gomez
Hey Dev, 

I was not able to understand the way to go on the code you posted. 


For you to familiarize and be able to explain me a bit better, here is where i am so far:
 
<apex:page standardController="CanaryAMS__Insurance_Product__c" renderAs="PDF">
<table style="width: 100%;">
<tbody>
<tr>
<td style="width: 265px;"><img style="float: left;" src="https://c.na34.content.force.com/servlet/servlet.FileDownload?file=01561000000DXcV" alt="" width="227" height="54" /></td>
<td style="text-align: right; width: 467px;">
<p>Phone: {!$Organization.Phone}<br />Fax: {!$Organization.Fax}<br />Website: www.InsuranceMedics.com</p>
<p>{!CanaryAMS__Insurance_Product__c.Name}</p>
</td>
</tr>
</tbody>
</table>
<hr />
<div  style="text-align: right;"> Date: 
<apex:outputText value="{0,date, MM'/'dd'/'yyyy}">
    <apex:param value="{!TODAY()}" /> 
</apex:outputText>
</div>
<apex:relatedList list="CanaryAMS__Carrier_Quotes__r" />
<p>&nbsp;</p>

<p>&nbsp;</p>
</apex:page>


"<apex:relatedList list="CanaryAMS__Carrier_Quotes__r" />" will give me what shows on that related list exactly as it shows on my record.

I am actually trying to use some of the fields of the related records that meet certain criteria in the vf page. 

that the step i am precisely blocked at. 

again, THANK YOU so much for your help.