• Sayandip ghosh
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hi i am having a custom button,which when clicked generates the PDF and saves it in attachment.
Now i am trying to make it as email too.
so when the custom button is clicked,generate pdf ,save the pdf to attachment and email it to particular user email.

How can i do it?Can i do it on single button?Or do i need two buttons. 1 .generate and save
2.email pdf?
I am confused and struck here .I am able to generate it and save as attachment.
But i want all three steps(generate,save and email) in single button.Please help.
Thanks in advance.

I'm writing a method to create a PDF Purchase Order and add it as an attachment to the PO record (custom object).

Here's the relavent apex code:

 

 public PageReference ViewPDF()
    {      
        system.debug('m_currentId = '+m_currentId);
        List<SFDC_Purchase_Requisition__c> m_reqResultList = new List<SFDC_Purchase_Requisition__c>();
        PageReference retPDF = new PageReference('/apex/PDFPO?id='+m_currentId);
        system.debug('m_myPO = '+ m_myPO);
        string PDFqueryPO = 'select Name, CreatedBy.name, Ship_To_Location__c,Expedite__c,PO_Date__c, Owner.id, Total_Price__c, PO_Notes__c from SFDC_Purchase_Order__c where Id = :m_currentId Limit 1';
        
        SFDC_Purchase_Order__c PDFPO = new SFDC_Purchase_Order__c();
        PDFPO =(SFDC_Purchase_Order__c)database.query(PDFqueryPO);
        string PDFReqResultListquery = m_RequestQueryByPOid;
        m_reqResultList= database.query(PDFReqResultListquery);
        m_displayResultList = new List<SFDC_Purchase_Requisition__c>();
        For(SFDC_Purchase_Requisition__c x : m_reqResultList)
        {
            if(x != null){
            x = (SFDC_Purchase_Requisition__c)x;
            m_displayResultList.add(x);
            }
           m_PDFPage = attach();
        }
        return m_PDFPage;
    }
      
    public PageReference attach() 
    {
         system.debug('m_currentId = '+m_currentId);
        Attachment myAttach = new Attachment();
         system.debug('m_currentId = '+m_currentId);
        myAttach.ParentId = m_currentId;    //Id of the object to which the page is attached
        if(m_myPO != null)
        {
            string myFileName = (m_myPO.name + ' - '+ date.TODAY() + '.PDF');
            myAttach.name = myFileName;
            PageReference myPdf = new PageReference('/apex/PDFPO?id=' + m_currentID);
            
            
            myAttach.body = myPdf.getContentAsPdf();
            try 
            {
                database.SaveResult sra = database.insert(myAttach);
                if(sra.issuccess() == true)
                {
                m_currentAttachId = sra.getID();
                }
                else
                {
                    // Operation failed, so get all errors                
                    for(Database.Error err : sra.getErrors()) 
                    {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('Attachment fields that affected this error: ' + err.getFields());
                    } 
                } 
            } catch(Exception e){
                system.debug('Creation of PDF Failed');
                system.debug('Message was: '+ e);
        }
    }
        return m_PDFPage;
    }

 And here's the visualforce page 

 

<apex:page standardController="SFDC_Purchase_Order__c" extensions="AttachPRTOPOExtension" name="PDFPO" renderAs="PDF" action="{!ViewPDF}">
	<apex:outputPanel id="POInfo" layout="Block" style="font:Arial font-size: 9pt">
		<apex:outputField value="{!SFDC_Purchase_Order__c.Name}"/>
		<br />
		<apex:outputField value="{!SFDC_Purchase_Order__c.PO_Date__c}"/>
		<apex:outputField value="{!SFDC_Purchase_Order__c.Status__c}"/>
		<br />
		<apex:outputField value="{!SFDC_Purchase_Order__c.Ship_To_Location__c}"/>
		<apex:outputField value="{!SFDC_Purchase_Order__c.Expedite__c}"/>
		<br />
        <br />
	</apex:outputPanel>     
	<apex:DataTable value="{!m_displayResultList}" var="r" border="1px" cellpadding="1px" cellspacing="2px">
		<apex:column width="10%" headerValue="Request" value="{!r.name}" />
		<apex:column width="10%" headerValue="Requested By" value="{!r.Createdby.name}"/>
		<apex:column width="10%" headerValue="Request Date" value="{!r.CreatedDate}"/>
		<apex:column width="10%" headerValue="Quantity" value="{!r.Quantity__c}" />
		<apex:column width="10%" headerValue="Item Requested" value="{!r.Item_Requested__r.Name}" />
		<apex:column width="10%" headerValue="Price" value="{!r.Item_Requested__r.Price_per_unit__c}" />
		<apex:column width="10%" headerValue="Extended Price" value="{!r.Extended_Price__c}" />
		<apex:column width="10%" headerValue="Receive-by Date" value="{!r.Receive_by_Date__c}" />
	</apex:DataTable>
	<apex:relatedList list="Purchase_Requisitions__r" Title="Requisitions connected with this PO"/>
     <apex:relatedList list="NotesandAttachments" Title="Notes and Attched PDF PO"/>
</apex:page>

 I'm compiling it as version 25, to avoid the bug earlier reported to Salesforce.  

 

Can someone explain what I'm doing wrong in my controller extension?  This is the first time I've tried to generate a PDF file and attach it to a record, so any insight would be very appreciated.

Thanks in advance.