0

My requirement is whenever we update a record its updated version should be saved in attachments with date and time. I'm good with it but I'm using two VF pages one for edit page and other as detail page (GeneratingPdfPage2) for input to convert to pdf ("Here is my doubt why cant I use standard record detail page for imput to convert to pdf?") Some one please help me can I do this with out 2nd vf page. if I use same edit page for pdf purpose output pdf is getting all picklist values into it. Here is my apex code:

public class generatePdfAndAttach {
string projectRecordId{get; set;}
private final Account acct{get; set;}
public PageReference Save() {
    Try{
        system.debug('inside save and acc++'+acct);
          upsert (acct);
        system.debug('after upsert');
          generatePdf();
        system.debug('after generate pdf');
        }
     catch(system.dmlException e) 
       {
         Apexpages.AddMessages(e);
          return null;
       }

     return new PageReference('/'+projectRecordId);
}


public generatePdfAndAttach(ApexPages.StandardController standardPageController) {
    this.acct = (Account)standardPageController.getRecord();
    projectRecordId  = ApexPages.CurrentPage().getparameters().get('id');
}

public PageReference generatePdf(){
    Attachment att = new Attachment();
    PageReference pdfPage = Page.GeneratingPdfPage2;//new PageReference('/'+projectRecordId);//Page.GeneratingPdfPage2;
    pdfPage.getParameters().put('id',projectRecordId);
    DateTime dt = datetime.now();
    att.name =''+acct.name+' - '+dt+'.pdf';
    // the contents of the attachment from the pdf
    // return page content as blob type
    Blob body;
    try {
    // returns the output of the page as a PDF
    body = pdfPage.getContentAsPDF();
    }
    catch (VisualforceException e) {
    body = Blob.valueOf('Can\'t get content as pdf');
    }
    att.body = body;
    att.IsPrivate = false;
    // Associate with project's record Id
    att.parentid = projectRecordId;
    att.ContentType = 'application/pdf';
    insert att;
    return new PageReference('/'+projectRecordId);
}

}

vf page 1:

<apex:page standardcontroller="Account" extensions="generatePdfAndAttach">
<apex:messages />
<apex:sectionheader title="{!$ObjectType.Account.label} Edit" subtitle="{!IF(ISNULL(Account.Name), 'New Account',Account.Name)}"/>
<apex:form >
    <apex:pageblock mode="edit" title="{!$ObjectType.Account.label} Edit">
        <apex:pageblockbuttons >
            <apex:commandbutton value="Save" action="{!Save}" />
            <apex:commandbutton value="Cancel" action="{!Cancel}"/>
        </apex:pageblockbuttons>
        <!-- **********   [Record Type : Master ]   **********  -->
        <apex:outputpanel >
            <apex:pageblocksection title="Account Information" showheader="true" columns="2">
                <apex:outputfield value="{!Account.OwnerId}"/>
                <apex:inputfield value="{!Account.Phone}" required="false"/>
                <apex:inputfield value="{!Account.Name}" required="true"/>
                <apex:inputfield value="{!Account.Fax}" required="false"/>
                <apex:inputfield value="{!Account.ParentId}" required="false"/>
                <apex:inputfield value="{!Account.Rating}" required="false"/>
                <apex:inputfield value="{!Account.AccountNumber}" required="false"/>
                <apex:inputfield value="{!Account.Website}" required="false"/>
                <apex:inputfield value="{!Account.Site}" required="false"/>
                <apex:inputfield value="{!Account.TickerSymbol}" required="false"/>
                <apex:inputfield value="{!Account.Type}" required="false"/>
                <apex:inputfield value="{!Account.Ownership}" required="false"/>
                <apex:inputfield value="{!Account.Industry}" required="false"/>
                <apex:inputfield value="{!Account.NumberOfEmployees}" required="false"/>
                <apex:inputfield value="{!Account.AnnualRevenue}" required="false"/>
                <apex:inputfield value="{!Account.Sic}" required="false"/>
            </apex:pageblocksection>
            <apex:pageblocksection title="Address Information" showheader="true" columns="2">
                <apex:inputfield value="{!Account.BillingStreet}" required="false"/>
                <apex:inputfield value="{!Account.ShippingStreet}" required="false"/>
                <apex:inputfield value="{!Account.BillingCity}" required="false"/>
                <apex:inputfield value="{!Account.ShippingCity}" required="false"/>
                <apex:inputfield value="{!Account.BillingState}" required="false"/>
                <apex:inputfield value="{!Account.ShippingState}" required="false"/>
                <apex:inputfield value="{!Account.BillingPostalCode}" required="false"/>
                <apex:inputfield value="{!Account.ShippingPostalCode}" required="false"/>
                <apex:inputfield value="{!Account.BillingCountry}" required="false"/>
                <apex:inputfield value="{!Account.ShippingCountry}" required="false"/>
            </apex:pageblocksection>
            <apex:pageblocksection title="Additional Information" showheader="true" columns="2">
                <apex:inputfield value="{!Account.CustomerPriority__c}" required="false"/>
                <apex:inputfield value="{!Account.SLA__c}" required="false"/>
                <apex:inputfield value="{!Account.SLAExpirationDate__c}" required="false"/>
                <apex:inputfield value="{!Account.SLASerialNumber__c}" required="false"/>
                <apex:inputfield value="{!Account.NumberofLocations__c}" required="false"/>
                <apex:inputfield value="{!Account.UpsellOpportunity__c}" required="false"/>
                <apex:inputfield value="{!Account.Active__c}" required="false"/>
                <apex:pageblocksectionitem />
            </apex:pageblocksection>
            <apex:pageblocksection title="Description Information" showheader="true" columns="1">
                <apex:inputfield value="{!Account.Description}" required="false"/>
            </apex:pageblocksection>
        </apex:outputpanel>
    </apex:pageblock>
</apex:form>

vf page 2 (GeneratingPdfPage2):

<apex:page standardcontroller="Account" sidebar="false" showHeader="false" showChat="false">
<apex:messages />
<apex:sectionheader title="{!$ObjectType.Account.label} Detail" subtitle="{!Account.Name}"/>
<chatter:feedwithfollowers entityId="{!Account.Id}"/>
<apex:form >
    <apex:pageblock mode="maindetail" title="{!$ObjectType.Account.label} Detail">
        <apex:pageblockbuttons >
            <apex:commandbutton value="Edit" action="{!Edit}"/>
            <apex:commandbutton value="Delete" action="{!Delete}"/>
        </apex:pageblockbuttons>
        <!-- **********   [Record Type : Master ]   **********  -->
        <apex:outputpanel >
            <apex:pageblocksection title="Account Information" showheader="false" collapsible="false" columns="2">
                <apex:pageblocksectionitem >
                    <apex:outputlabel value="Account Owner"/>
                    <apex:outputpanel >
                        <apex:outputfield value="{!Account.OwnerId}"/>
                        <apex:outputlink value="{!URLFOR($Action.Account.ChangeOwner,Account.id)}" rendered="{!NOT(ISNULL(Account.Id))}">[Change]</apex:outputlink>
                    </apex:outputpanel>
                </apex:pageblocksectionitem>
                <apex:outputfield value="{!Account.Phone}"/>
                <apex:outputfield value="{!Account.Name}"/>
                <apex:outputfield value="{!Account.Fax}"/>
                <apex:outputfield value="{!Account.ParentId}"/>
                <apex:outputfield value="{!Account.Rating}"/>
                <apex:outputfield value="{!Account.AccountNumber}"/>
                <apex:outputfield value="{!Account.Website}"/>
                <apex:outputfield value="{!Account.Site}"/>
                <apex:outputfield value="{!Account.TickerSymbol}"/>
                <apex:outputfield value="{!Account.Type}"/>
                <apex:outputfield value="{!Account.Ownership}"/>
                <apex:outputfield value="{!Account.Industry}"/>
                <apex:outputfield value="{!Account.NumberOfEmployees}"/>
                <apex:outputfield value="{!Account.AnnualRevenue}"/>
                <apex:outputfield value="{!Account.Sic}"/>
                <apex:outputfield value="{!Account.AccountStatus__c}"/>
                <apex:pageblocksectionitem />
                <apex:outputfield value="{!Account.Potential_Value__c}"/>
                <apex:pageblocksectionitem />
            </apex:pageblocksection>
            <apex:pageblocksection title="Address Information" showheader="false" collapsible="false" columns="2">
                <apex:pageblocksectionitem >
                    <apex:outputlabel value="Billing Address"/>
                    <apex:outputpanel >
                        <apex:outputfield value="{!Account.BillingStreet}"/>
                        <br/>&nbsp;


                        <apex:outputfield value="{!Account.BillingCity}"/>&nbsp;,&nbsp; 

                        <apex:outputfield value="{!Account.BillingState}"/>&nbsp; 

                        <apex:outputfield value="{!Account.BillingPostalCode}"/>&nbsp;

                        <br/>
                        <apex:outputfield value="{!Account.BillingCountry}"/>
                    </apex:outputpanel>
                </apex:pageblocksectionitem>
                <apex:pageblocksectionitem >
                    <apex:outputlabel value="Shipping Address"/>
                    <apex:outputpanel >
                        <apex:outputfield value="{!Account.ShippingStreet}"/>
                        <br/>&nbsp;


                        <apex:outputfield value="{!Account.ShippingCity}"/>&nbsp;,&nbsp; 

                        <apex:outputfield value="{!Account.ShippingState}"/>&nbsp; 

                        <apex:outputfield value="{!Account.ShippingPostalCode}"/>&nbsp;

                        <br/>
                        <apex:outputfield value="{!Account.ShippingCountry}"/>
                    </apex:outputpanel>
                </apex:pageblocksectionitem>
            </apex:pageblocksection>
            <apex:pageblocksection title="Additional Information" showheader="false" collapsible="false" columns="2">
                <apex:outputfield value="{!Account.CustomerPriority__c}"/>
                <apex:outputfield value="{!Account.SLA__c}"/>
                <apex:outputfield value="{!Account.SLAExpirationDate__c}"/>
                <apex:outputfield value="{!Account.SLASerialNumber__c}"/>
                <apex:outputfield value="{!Account.NumberofLocations__c}"/>
                <apex:outputfield value="{!Account.UpsellOpportunity__c}"/>
                <apex:outputfield value="{!Account.Active__c}"/>
                <apex:pageblocksectionitem />
            </apex:pageblocksection>
            <apex:pageblocksection title="System Information" showheader="false" collapsible="false" columns="2">
                <apex:pageblocksectionitem >
                    <apex:outputlabel value="Created By"/>
                    <apex:outputpanel >
                        <apex:outputfield value="{!Account.CreatedById}"/>&nbsp;, 

                        <apex:outputfield value="{!Account.CreatedDate}"/>
                    </apex:outputpanel>
                </apex:pageblocksectionitem>
                <apex:pageblocksectionitem >
                    <apex:outputlabel value="Last Modified By"/>
                    <apex:outputpanel >
                        <apex:outputfield value="{!Account.LastModifiedById}"/>&nbsp;, 

                        <apex:outputfield value="{!Account.LastModifiedDate}"/>
                    </apex:outputpanel>
                </apex:pageblocksectionitem>
            </apex:pageblocksection>
            <apex:pageblocksection title="Description Information" showheader="false" collapsible="false" columns="1">
                <apex:outputfield value="{!Account.Description}"/>
            </apex:pageblocksection>
        </apex:outputpanel>
    </apex:pageblock>
</apex:form>
<!-- **********   [Related Lists for Record Type : Master ]   **********  -->
<apex:outputpanel >
    <apex:relatedlist list="Contacts" title="Contacts"/>
    <apex:relatedlist list="AccountContactRoles" title="Contact Roles"/>
    <apex:relatedlist list="AccountTeamMembers" title="Account Team"/>
    <apex:relatedlist list="Opportunities" title="Opportunities"/>
    <apex:relatedlist list="Cases" title="Cases"/>
    <apex:relatedlist list="OpenActivities" title="Open Activities"/>
    <apex:relatedlist list="CombinedAttachments" title="Notes & Attachments"/>
    <apex:relatedlist list="ActivityHistories" title="Activity History"/>
    <apex:relatedlist list="AccountPartnersFrom" title="Partners"/>
    <apex:relatedlist list="ProcessSteps" title="Approval History"/>
</apex:outputpanel>

3 Answers 3

1

Then in page1 make all the fields as outputFields and use inlineEdit, which will then allow you edit the fields as and when needed and generate the pdf as well. Hope this works.

1
  • i tried that way too when I inline edit picklist it wont work. the pdf shows all the picklist items May 5, 2016 at 13:01
0

You need to set the attribute renderAs="pdf" on the page which you want to generate as pdf. <apex:page standardcontroller="Account" sidebar="false" showHeader="false" showChat="false" renderAs="pdf">

1
  • no no I think you didnt get the question, Im able to get the pdf file and attach it in attachments section but I'm using two vf pages one for editing and other for record detail input. I was asking can we do that with single vf page? May 4, 2016 at 13:24
0

Did you try it already? I don't see any problem with that. Instead of using page2 use page1 to generate the pdf. Call the generatepdf method in the save method before returning the new page reference. Make sure query all the fields the visualforce page is using.

1
  • if I use page1 as it is edit page, if it has any picklist like account rating, the pdf will show all the picklist values in it. did you see that? it means pdf file is taking edit page as it is May 5, 2016 at 7:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .