+ Start a Discussion

attach all child record as attachment to a parent with a custom button

Hi All,

We currently have a requirement to save all the child records of a parent as a pdf attachment to the parent record and send an email to internal employee with all the child attachments and the parent record too.

Parent object: Fact_Find__c
Child objects: Super_checklists__c,Investment_Checklist__c, Cash_Flow_Analysis__c,Insurance_Checklist__c, Life_Style_and_Financial_Goals__c.

Our requirement is to send an email to the internal employess with all these records attached to the email. including the parent record.

I just need to know is there any way to achieve this?

Please guide me in the right way.
Thanks in advance.
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.

Hi Venky,

Thanks for your prompt response. I was thinking to go in the same way but was a bit unsure. Thanks for your suggestion. Would you be able to help me out in coding this? I came across some Apex code in http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/  (http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/)  

I am trying to start from this. But I dont have idea how to do this for child objects. Sometimes there might be multiple child records for a single child object. Would you be able to help me by modifying this code?

public with sharing class PdfGeneratorController {

  public ID parentId {get;set;}
  public String pdfName {get;set;}

  public PageReference savePdf() {

    PageReference pdf = Page.PdfGeneratorTemplate;
    // add parent id to the parameters for standardcontroller

    // create the new attachment
    Attachment attach = new Attachment();

    // the contents of the attachment from the pdf
    Blob body;

    try {

        // returns the output of the page as a PDF
        body = pdf.getContent();

    // need to pass unit test -- current bug    
    } catch (VisualforceException e) {
        body = Blob.valueOf('Some Text');

    attach.Body = body;
    // add the user entered name
    attach.Name = pdfName;
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.ParentId = parentId;
    insert attach;

    // send the user to the account to view results
    return new PageReference('/'+parentId);



In the visualforce page PdfGeneratorTemplate, use apex:datatable or apex:pageblcktable to display related list records.
Hi Venky,

This will display the child records in the same page. can we have them as attachments instead so that I can trigger an email with all the attachments.