+ Start a Discussion
rakhi78rakhi78 

visual force page needs to be attached as pdf by controller class

 
/*
 *  This class which extends the Dispense__c standard controller is utilized by the refillrequest
 *  Visualforce page to default values for the user from the related refill request Page during 
 *  the creation of a new refill requset.  It also provides an action that generates the PDF and creates 
 *  an attachment under the parent dispense record who's id is passed into the same Visualforce page 
 *  that can be accessed in the UI to generate the same PDF.
 */
public class refillrequestExt {

    /* The standard controller object which will be used later for navigation and to invoke
       it's save action method to create the new refill request. */
    ApexPages.StandardController controller;
    private final Dispense__c dispense;
    /* The quote property which is used by the quoteNew and quotePDF Pages and this controller
       to provide access to the relevant quote information. */
    public Dispense__c d {get;set;}
    
    /* The constructor to this extension class which takes the standard controller as its argument
       which allows this class to access the methods and information available in the instance for 
       the refill request.*/
    public refillrequestExt(ApexPages.StandardController c) 
    {
        controller = c;
        d      =  (Dispense__c) c.getRecord();
        
         /* If non-null, get the opportunity and contact role for appropriate defaulting of values. */
        String theId = ApexPages.currentPage().getParameters().get('id');
        If(theId!= null)
        {
        dispense = [select Id, Name, Dispense__c.Prescription_ID__r.Pharmacy__r.Name, Dispense__c.Prescription_ID__r.Pharmacy__r.Main_Referral_Phone__c, Dispense__c.Pharmacy_Fax__c, Dispense__c.Patient_First_Name__c,Dispense__c.Patient_MRN__r.Patient_Address_1__c,Dispense__c.Patient_MRN__r.Patient_Address_2__c
                    from Dispense__c
                    where id = :theId];
        }
        //
     }
    public Dispense__c getDispense() 
    {
        return dispense;
    }
    
    /* This save method will be called instead of the standard controller save method when bound
       to an action component in an associated page, (apex:commandButton on the quoteNew 
       Visualforce page in this example) */
    public PageReference save() {
    
        /* Invoke the standard controller save method which returns the pageReference class 
           that will be used in the navigation, i.e. send the user to the expected location based on
           standard navigation rules provided by salesforce.com */
        PageReference p = controller.save();
        
        /* The dispense's record Id */
        id did = controller.getId();
        
        
               
        /* Return the page reference generated by the standard controller save, which usually drops the user
           on the detail page for the newly created object. */
        return p;
        
    }
    
    /* The action method that will generate a PDF document from the RefillRequestPDF page and attach it to 
       the dispense provided by the standard controller. Called by the action binding for the attachQuote
       page, this will do the work and take the user back to the quote detail page. */
    public PageReference attachRefillRequest() {
        /* Get the page definition */
        PageReference pdfPage = Page.RefillRequest;
        
        /* set the dispense id on the page definition */
        pdfPage.getParameters().put('id',d.id);
        
        /* generate the pdf blob */
        Blob pdfBlob = pdfPage.getContent();
        
        /* create the attachment against the dispense */
        Attachment a = new Attachment(parentId = d.id, name=d.name + '.pdf', body = pdfBlob);
        
        /* insert the attachment */
        insert a;
        
        /* send the user back to the dispense detail page */
        return controller.view();
    }
}
 This is my visual force page. when user clicks save the page needs to get converted to pdf and attch as a attachment in dispense_c object particualr record.
<apex:page id="RefillApproval" standardController="Dispense__c"  Extensions="refillrequestExt" showHeader="false" sidebar="false" >


<apex:form >

<table width="100%" columns="1">
<tr><td align="center"><img width="200" height="50" src="/servlet/servlet.ImageServer?id=015Q00000004m7k&oid=00DQ00000008eNT"/></td></tr>
<tr><td align="center"><apex:outputText ><h4>{!Dispense__c.Prescription_ID__r.Pharmacy__r.Name}</h4></apex:outputText></td></tr>
<tr><td align="center"><apex:outputText value="Phone: {!Dispense__c.Prescription_ID__r.Pharmacy__r.Main_Referral_Phone__c}"/></td></tr>
<tr><td align="center"><apex:outputText value="Fax: {!Dispense__c.Pharmacy_Fax__c}"/></td></tr>
</table><br></br>

<table width="100%" columns="2">
<tr><td width="50%"><apex:outputText ><h4>Patient:</h4></apex:outputText></td>
<td width="50%"><apex:outputText ><h4>Physician:</h4></apex:outputText></td></tr>
<tr><td width="50%"><apex:outputText value="{!Dispense__c.Patient_First_Name__c} {!Dispense__c.Patient_Last_Name__c}"/></td>
<td width="50%"><apex:outputText value="{!Dispense__c.Prescription_ID__r.Physician_First_Name__c} {!Dispense__c.Prescription_ID__r.Physician_Last_Name__c}"/></td></tr>
<tr><td width="50%"><apex:outputText value="{!Dispense__c.Patient_MRN__r.Patient_Address_1__c} {!Dispense__c.Patient_MRN__r.Patient_Address_2__c}"/></td>
<td width="50%"><apex:outputText value="{!Dispense__c.Prescription_ID__r.Physician_Address_1__c} {!Dispense__c.Prescription_ID__r.Physician_Address_2__c}"/></td></tr>
<tr><td width="50%"><apex:outputText value="{!Dispense__c.Patient_MRN__r.Patient_City__c}, {!Dispense__c.Patient_MRN__r.Patient_State_Province__c} {!Dispense__c.Patient_MRN__r.Patient_Zip_Postal_Code__c}"/></td>
<td width="50%"><apex:outputText value="{!Dispense__c.Prescription_ID__r.Physician_City__c}, {!Dispense__c.Prescription_ID__r.Physician_State__c} {!Dispense__c.Prescription_ID__r.Physician_Zip_Code__c}"/></td></tr>
<tr><td width="50%"><apex:outputText value="DOB: {!Dispense__c.Patient_MRN__r.Birthdate__c}"/></td>
<td width="50%"><apex:outputText value="Phone: {!Dispense__c.Prescription_ID__r.Physician_Phone__c}"/></td></tr>
<tr><td width="50%"><apex:outputText value="Phone: {!Dispense__c.Patient_MRN__r.Primary_Phone__c}"/></td>
<td width="50%"><apex:outputText value="Fax: {!Dispense__c.Prescription_ID__r.Physician_Fax__c}"/></td></tr>
<tr><td width="50%"><apex:outputText value="Sex: {!Dispense__c.Patient_MRN__r.Gender__c}"/></td>
<td width="50%"><apex:outputText value="DEA No: {!Dispense__c.Prescription_ID__r.Physician_DEA__c}"/></td></tr>
<tr><td width="50%"><apex:outputText value="MRN: {!Dispense__c.Patient_MRN__r.Name}"/></td></tr>
</table><br></br>

<table width="100%" columns="1" align="center">
<tr><td align="center"><apex:outputText ><h4>REFILL REQUEST - {!Dispense__c.Drug_Compound_Name__c}</h4></apex:outputText></td></tr>
<tr><td align="center"><apex:outputText value="Patient has been receiving this medication and the last refill was {!Dispense__c.Last_Dispensed__c}."/></td></tr>
</table><br></br>

<table width="100%" columns="2">
<tr><td colspan="2"><apex:outputText ><h4>Prescription:</h4></apex:outputText></td>
<td></td></tr>
<tr><td><apex:outputText value="Medication:"/></td>
<td><apex:inputText id="Medication" size="50" style="border: 2px" value="{!Dispense__c.Drug_Compound_Name__c}"/></td></tr>
<tr><td><apex:outputText value="Sig:"/></td>
<td><apex:inputText id="Sig" size="50" style="border: 2px" value="{!Dispense__c.Rx_Type__c} {!Dispense__c.Dose__c} {!Dispense__c.Frequency__c}"/></td></tr>
<tr><td><apex:outputText value="Dispense #:"/></td>
<td><apex:inputText id="Number" size="50" style="border: 2px" value="{!Dispense__c.Referral_Item_Quantity__c}"/></td></tr>
<tr><td><apex:outputText value="Refill x's:"/></td>
<td><apex:inputText id="Refills" size="50" style="border: 2px" value="{!Dispense__c.Total_Refills_Authorized__c}"/></td></tr>
<tr><td height="10" /></tr>
<tr><td><apex:outputText ><h4>Comments:</h4></apex:outputText></td>
<td><apex:inputTextarea cols="52" style="border: 2px" rows="2"/></td></tr>
<tr><td height="10" /></tr>
<tr><td><apex:outputText ><h4>Submitted by: </h4></apex:outputText></td>
<td><apex:outputText value="{!$User.FirstName} {!$User.LastName}"/></td></tr>
<tr><td><apex:outputText ><h4>Phone: </h4></apex:outputText></td>
<td><apex:outputText value="{!$User.Phone}"/></td></tr>
<tr><td><apex:outputText ><h4>Date: </h4></apex:outputText></td>
<td><apex:outputText value=" {!Today()}"/></td></tr>
<tr><td height="10" /></tr>
<tr><td colspan="2"><apex:outputText ><h4><i>Submitted from US Bioservices Physician Portal.</i></h4></apex:outputText></td></tr>
<tr><td height="10" /></tr>
<tr><td colspan="2"><apex:outputText >Submission Terms - By clicking the "Submit" button below, you certify your authorization to prescribe
<br>for this patient, approve the electronic submission of this form, and understand that a pharmacist
<br>may also follow up with you via phone.</br></br>
</apex:outputText></td></tr>
</table>

<table align="center" width="100%"><tr><td align="center">
<apex:commandButton value="Submit" title="Submit" action="{!attachRefillRequest}"/>
<apex:commandButton value="Print" title="Print Copy" onclick="navigateToUrl('javascript&colon;window.print%28%29%3B')"/>
<apex:commandButton value="Close" title="Close Window" onclick="navigateToUrl('javascript&colon;window.close%28%29%3B')"/>
</td></tr></table>

</apex:form>

</apex:page>