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
angela mullen-smith 18angela mullen-smith 18 

I have a visualforce page which extracts data from the Account object and displays it in a form how can I save that form as an attachment

I have a button which creates a Visual force form that extracts data from the Account Object and displays it in a presentation format. 
How can I create an action to Save that form that I have created to the Account record and also to a Share point folder
Khan AnasKhan Anas (Salesforce Developers) 
Hi Angela,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce 1: GeneratePdfFormAttachC
<apex:page standardcontroller="Account" extensions="GeneratePdfFormAttachC">
    <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>
</apex:page>

Controller:
public class GeneratePdfFormAttachC {
    
    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 GeneratePdfFormAttachC(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);
    }
}

Visualforce 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:pageblocksectionitem />
                    <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="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>
</apex:page>

Finally, add the first visualforce in a button on Account object and add it to page layout.

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. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
angela mullen-smith 18angela mullen-smith 18
Hi
Thanks for providing this - I will need to digest it all