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
lalitha_sfdclalitha_sfdc 

Generate the Pdf of the record after saving and attach it to the record itself ?

Hi, 
I need to Generate the Pdf of the record after saving and attach it to the record itself. The pdf file has to be updated with the date and time after every edit. Please share with an example.
 
Best Answer chosen by lalitha_sfdc
lalitha_sfdclalitha_sfdc
I have tried something this way by basic programming 

apex class:
public class samplepdf1 {
public list<account> accountrecord{get;set;}
     public string accountid{get;set;}
    public account acc{get;set;}
 public samplepdf1(ApexPages.StandardController stdController){
 accountid = ApexPages.currentPage().getParameters().get('id');
   this.acc = (Account)stdController.getRecord();
    }    
    public pagereference save()
    {
             upsert acc;
              savepdf();
        // send the user to the account to view results
        return new PageReference('/'+accountid);
    }
    public PageReference savepdf() {
    PageReference pdf = Page.samplepdf1vf;//new PageReference('/'+accountid);Page.samplepdf1vf;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',accountid);
    // 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.getContentasPDF();
      } catch (VisualforceException e) {
        body = Blob.valueOf('unable to get content as pdf');
    }
    attach.Body = body;
    // add the user entered name
    attach.Name = acc.name+'.pdf';
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.parentid = accountid;
    attach.ContentType = 'application/pdf';
    insert attach;
     return null;
  } 
}

vf page: 
 
<apex:page standardcontroller="Account" extensions="samplepdf1">
  <...>
        </apex:page>

have a look this may be helpful 

All Answers

Sagar PareekSagar Pareek
Here you go - https://success.salesforce.com/answers?id=90630000000gtkuAAA
lalitha_sfdclalitha_sfdc
I have tried something this way by basic programming 

apex class:
public class samplepdf1 {
public list<account> accountrecord{get;set;}
     public string accountid{get;set;}
    public account acc{get;set;}
 public samplepdf1(ApexPages.StandardController stdController){
 accountid = ApexPages.currentPage().getParameters().get('id');
   this.acc = (Account)stdController.getRecord();
    }    
    public pagereference save()
    {
             upsert acc;
              savepdf();
        // send the user to the account to view results
        return new PageReference('/'+accountid);
    }
    public PageReference savepdf() {
    PageReference pdf = Page.samplepdf1vf;//new PageReference('/'+accountid);Page.samplepdf1vf;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',accountid);
    // 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.getContentasPDF();
      } catch (VisualforceException e) {
        body = Blob.valueOf('unable to get content as pdf');
    }
    attach.Body = body;
    // add the user entered name
    attach.Name = acc.name+'.pdf';
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.parentid = accountid;
    attach.ContentType = 'application/pdf';
    insert attach;
     return null;
  } 
}

vf page: 
 
<apex:page standardcontroller="Account" extensions="samplepdf1">
  <...>
        </apex:page>

have a look this may be helpful 
This was selected as the best answer