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
mahesh p 54mahesh p 54 

how to add functionality to a single button of downloading pdf,sending email and attaching as an attachment on a custom object record

<apex:page extensions="SaveAndOpenPDF" renderAs="{!renderAs}" standardController="Booking__c">
PDF Content Goes Here
<apex:form >
    
    
    
    <apex:outputField value="{!Booking__c.Customer__r.Name}"></apex:outputField>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <apex:outputField value="{!Booking__c.Name}"></apex:outputField><br/>
    
    
    <apex:pageBlock title="Customer">

            <apex:pageBlockSection columns="2" title="Basic Information"> 
                <apex:outputField value="{!Booking__c.Customer__r.Name}"/>
                <br/>
                <apex:outputField value="{!Booking__c.Customer__r.Address_1__c}"/>
                <apex:outputField value="{!Booking__c.Customer__r.Address_2__c}"/>
                <apex:outputField value="{!Booking__c.Customer__r.Country__c}"/>
                <apex:outputField value="{!Booking__c.Customer__r.States__c}"/>
                <apex:outputField value="{!Booking__c.Customer__r.city__c}"/>
                <apex:outputField value="{!Booking__c.Customer__r.Zip_Code__c}"/>
                <apex:outputField value="{!Booking__c.Customer__r.Account__c}"/>
                <apex:outputField value="{!Booking__c.Customer__r.Customer_Email__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="2" title="Tour Information"> 
                <apex:outputField value="{!Booking__c.Customer__r.Passport_Number__c}"/>                
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="2" title="Health Information"> 
                <apex:outputField value="{!Booking__c.Customer__r.Blood_Group__c}"/>                
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1" title="Tour Packages" > 
            
                <apex:pageBlockTable value="{!Booking__c}" var="b">            
                <apex:column headerValue="Type a Destination">
                    <apex:outputField  value="{!Booking__c.Type_a_destination__c}"/>
                </apex:column>
                <apex:column headerValue="Travel Mode">
                    <apex:outputField  value="{!Booking__c.Travel_Mode__c}"/>
                </apex:column>
                <apex:column headerValue="From Date">
                    <apex:outputField  value="{!Booking__c.From_Date__c}"/>
                </apex:column>
                <apex:column headerValue="To Date">
                    <apex:outputField  value="{!Booking__c.To_Date__c}"/>
                </apex:column>
                <br/>
                <apex:column headerValue="Bookings for Customer">
                    <apex:outputField  value="{!Booking__c.Customer__c}"/>
                </apex:column>
            
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
        </apex:pageBlock>
    
</apex:form>
</apex:page>
public with sharing class SaveAndOpenPDF {
    
    public final Customer__c customer;
    public SaveAndOpenPDF(){
        customer = [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c from Customer__c where id=:ApexPages.currentPage().getParameters().get('id') ];//= :ApexPages.currentPage().getParameters().get('id') 
    }    
    public Customer__c getCustomer() {
        return customer;
    }
  public String recordId {
    get {
      return ApexPages.currentPage().getParameters().get('Id');
    }
  }
    
    //String blobVal = EncodingUtil.base64Encode(pdfbody);  
    //String pdfBlobString = 'data:application/pdf;content-disposition:attachment;base64,'+blobVal;
    
    public ApexPages.StandardController stdCntrlr {get; set;}

    public SaveAndOpenPDF(ApexPages.StandardController controller) {
    stdCntrlr = controller;
    }

  // this is to make testing a lot easier -- simply append renderAs=html
  // to the url parameters to test, add displayOnly=1 if you don't want to save
  public String renderAs {
    get {
      if (String.isBlank(ApexPages.currentPage().getParameters().get('renderAs'))) {
        return 'pdf';
      } else {
        return ApexPages.currentPage().getParameters().get('renderAs');
      }
    }
  }

  public PageReference saveAndOpenPDF() {
    if (String.isBlank(ApexPages.currentPage().getParameters().get('displayOnly'))) {
      Id attachmentId = savePDF();
      return openPDF(attachmentId);
    } else {
      return null;
    }
  }

  public Id savePDF() {
    Attachment attachment = new Attachment();
    attachment.ParentId = recordId;
    attachment.name = 'Customer_Booking_Details_PDF_'+String.valueof(Datetime.now())+'.pdf';
    PageReference pdf = Page.SaveAndOpenPDF;
    pdf.getParameters().put('Id', recordId);
    pdf.getParameters().put('displayOnly', '1');
    pdf.setRedirect(true);
    try {
      attachment.Body = pdf.getContent();
    }
    catch (VisualForceException e) {
      attachment.Body = Blob.valueof('There was an error.');
    }
    attachment.ContentType = 'application/pdf';
    insert attachment;
    return attachment.Id;
  }

  public PageReference openPDF(Id attachmentId) {
    PageReference ret = new PageReference('/servlet/servlet.FileDownload?file=' + attachmentId);
    ret.setRedirect(true);
    return ret;
  }    
}

I created a custom button named Generate Itinerary on Booking Custom object page layout.When i click that I can view PDF with the details which i put in my VF page but now i need to add features like send an email to custom field and attach it to the booking custom object with the single button Generate Itinerary.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Mahesh,

Greetings to you!

Please refer to the below link with a similar discussion which might help you further with the above issue.

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


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
mahesh p 54mahesh p 54
I already saw that link but i couldn't figure out how to add those features in my apex class and VF page.If you could help me in doing that highly appreciated