Generating Documents in Salesforce natively using Visualforce

In the Salesforce world this is the age of Process Builders, Flows, Lightning components and Lightning web components. People forget Visualforce is still a technology that is supported by Salesforce Lightning.

A very common requirement for users is to generate a PDF or a Word document based on the Salesforce data. Examples of such requirements are: Generate an invoice based on Order/Opportunity, generate a quote for an Opportunity, an acknowledgment letter for a donation received by a NonProfit. There are numerous apps on AppExchange that have this functionality. However, generation of documents from Salesforce based on data in Salesforce can be done natively using Visualforce. In this article we talk about how Visualforce can be used to generate business documents with information from records natively in Salesforce Lightning.

One of the major challenges that we face using any software application is to export/convert data into other formats and later using them.

Salesforce provides us an easy method for generating a document just by using Visualforce page and Apex. Even people not having much experience with Visualforce or Apex can easily generate a document in a PDF format by just adding renderAs attribute in the component, and specifying ‘’pdf’’ as the rendering service as shown below.

Generating-Document-in-Salesforce

RenderAs -This keyword is used with page components and it converts the page in the specified format. Rendering a VF page as pdf is for the pages that are designed for print. Standard components like inputs and buttons which require JavaScript to be formatted should not be used as they are not easily formatted for print. Depending upon the browser settings, the document will either be displayed or downloaded.

Let’s have a look at a simple example of generating a document in pdf format:

Generating-Document-in-Salesforce
Figure 1: Display of basic PDF using Visualforce

Code Snippet:

<apex:page renderAs="pdf" > <h1 > DOCUMENT </h1> <h2>Your first PDF is ready</h2> See, how easy it is to create a document! </apex:page>
You can download the generated document just by clicking the download button on the pdf on the right-hand side top corner.
Now, as you have learned to create a document using Visualforce, let’s take a step further and do something interesting.

So, our VF page is ready, but what if we want to add in Salesforce Data!
This feature can be used to fulfill any kind of requirement that you want to print on a paper or develop a document. Let’s suppose you are working on a project and you need to get the details of records from any particular object to be present in the form of a document, you just have to add the renderAs attribute to your Visualforce page and you will have the document ready to be downloaded. By applying CSS, you can format your PDF document the way you need.
Let’s have a look at another example, where we are fetching details of records from a Standard Object ‘Contact’ and displaying them in a table in PDF document:
This is how our table of records look that we fetched straight from the database:

VF PAGE:
Generating-Document-in-Salesforce
Figure 2: List of contacts displayed using Visualforce in Salesforce

code snipped

OTHER FORMATS

Unfortunately, you can only specify the PDF format in the renderAs attribute at the present. If you want some other Document format , you can use contentType attribute in place of renderAs and you can state the format name you want in it. The ContentType attribute takes a Multipurpose Internet Mail Extension (MIME) media type as a value, such as MS-Excel/Doc.

  • Remove “RenderAs” attribute
  • Add a new attribute to the page tag: “ContentType”:

If your requirement is to create a WORD file, the entire process would be the same only following changes are required:

contentType="application/msword#FileName.doc"

Output:
Generating-Document-in-Salesforce
Figure 3: Display of Word file using Visualforce

If your requirement is to create an EXCEL file, the entire process would be the same - only following changes are required: contentType=" application/x-excel#FileName.xls"

Output:
Generating-Document-in-Salesforce
Figure 4: Display of Excel file using Visualforce

After doing the changes described above, your file is automatically downloaded and from there you can use it and send it as an attachment in the mail. It is as simple as that! But, there can be some unpredictable behaviour in the browser if the contentType is set invalid. As a bonus, this approach allows you to specify a filename. You write the document name after the ‘#’ , and the document will be downloaded with the file name you specify.

EXAMPLE - contentType="application/msword#MyContactInfo.doc"

THINGS TO KEEP IN MIND

  • When VF pages are rendered as pdf, Visuaforce charts aren’t displayed.
  • And apex input or HTML form elements are not included in the document
  • For a professional look, you can also apply CSS classes and styling to the VF page.
  • Depending upon the browser settings, the generated document is either displayed or downloaded automatically.
  • Set the “ShowHeader” attribute to false in the page tag for not rendering any HTML tags or Salesforce header.

CONCLUSION

There are many different ways to generate documents in Salesforce natively using Visualforce, but this method provided by the salesforce of converting HTML to PDF is user-friendly, easy and a better option, due to sheer control over the end results.
For any additional queries on generating documents natively in Salesforce please contact us at support@astreait.com

REFERENCES