Build a PDF template
in this tutorial, you'll learn how to turn a fillable pdf form into a reusable template for automated document generation in salesforce by mapping pdf fields to salesforce data, cloudfiles docgen ensures that information flows automatically into the right places, eliminating manual entry and reducing errors across all generated documents what to expect by the end of this tutorial, you will learn how to configure a fillable pdf template using actions like insert field docid\ orfoyrd5cikhqt8gizahd and insert condition docid 4rtfqefk4uvthpvsxmy2y to map salesforce data publish the template for both manual use (from record pages) and automated use (with salesforce flows or processes) prerequisites install cloudfiles in salesforce cloudfiles is available on the salesforce appexchange and can be installed using the standard app installation process check out installation docid\ aw06gffsiiq qdp1fkgcj and configuration docid\ zgbv7ll pzzzopel4 7up guides to set up your account and user access have a fillable pdf form ready to build a template in cloudfiles, you'll need a fillable pdf form a fillable pdf is not just a static document — it's a pdf that has interactive fields such as text boxes and checkboxes layered on top of the form design these fields can capture or display values, which makes them perfect for mapping to salesforce data during document generation common examples of fillable pdfs include government forms (e g , w 9, i 9, tax forms) application forms (e g , bank loans, vendor onboarding) standard business contracts with input fields if your pdf doesn't already contain fillable fields, you can add them using tools like adobe acrobat (prepare form) or other pdf form editors before uploading it to cloudfiles building the template for this tutorial, we'll use the irs w 9 form as an example the w 9 is widely used by businesses to collect tax related information from vendors or organizations we'll connect this form to the account object in salesforce, since most of the details required on a w 9, such as business name, address, tax classification, and ein are typically stored in account records along with some custom fields this makes accounts the most natural fit for generating w 9s automatically 1 upload a template steps to upload a pdf form steps to upload a pdf form in salesforce, open the cloudfiles app and go to the document generation tab click upload template and select your irs w 9 pdf enter a template name (e g , "w 9 form – account") once uploaded, locate it in the list, click the menu icon (⋮), and choose edit in the side panel, select the account object and click set $record the pdf will now open in edit mode with edit , resources , preview , and publish tabs on the right panel, ready for mapping 2 insert text use insert text to place values from salesforce fields into text areas of the pdf map "account name" to "name (line 1)" click the + insert text button in the right panel click the name (line 1) field on the pdf, or select it from the field list in the bottom panel in the insert field section on the right, choose the field {!$record name} (account → name) from the field api name dropdown click apply map other fields as below business name (custom field) → business name/disregarded entity (line 2) billing street → address (line 5) billing city → city (line 6) 3 insert checkbox use insert checkbox to control checkboxes in the pdf based on salesforce data map "account type = individual" to "individual/sole proprietor" click the + insert checkbox button in the right panel click the checkbox for individual/sole proprietor on the pdf, or select it from the bottom panel in the condition builder on the right set field → account type ({!$record type}) set operator → equals enter value → individual click apply to confirm the condition map other fields as below if account type = corporation , tick c corporation if account type = partnership , tick partnership 4 use resources (optional) sometimes you’ll need data beyond the main object (account) template resources docid\ sx1 c9ittouclsjwhf4zw let you pull in values from related records (using a record single variable docid\ vwhs3pzv6unh2 77nrq7n ) or capture user provided inputs (using a runtime variable docid\ axlc5bzdbbimwpkbabj9 ) record single variable a record single variable is used when you need to bring values from a related or different object into your pdf template instead of being limited to the fields on the main object (e g , account), you can extend the template to include data from related records such as contacts, opportunities, or custom objects why use it here? in the w 9 form, the "requester's name and address (optional)" field is not typically stored on the account record instead, it may come from a related contact (e g , the primary requester) by using a record single variable, you can fetch that contact's details and map them into the optional field on the pdf steps to create and use a record single variable create the variable go to the resources tab → + create new resource select record single variable set the object as contact give it a label (e g , requester) add filter criteria add a condition account id = {!$record account id} this links the contact record to the account being used insert the variable the variable will now appear under record (single) variables in the field selector map to the pdf click the + insert text button, then click the requester's name and address (optional) field in the pdf in the field api name dropdown, choose the variable (e g , requester name) click apply this ensures cross object data like contact details can flow into the w 9 even when the template is tied to accounts (refer to record single variable for more detailed steps on mapping ) runtime variable a runtime variable docid\ axlc5bzdbbimwpkbabj9 stores a value that can be passed from a salesforce flow and generated dynamically during execution using doc generation flow actions docid\ nv27oftdk5ukedfzqpbpq it acts as a placeholder for data provided by the user at runtime, which can then be mapped to a field in the pdf template through apex defined variables why use it here? in the w 9 form, the "other (see instructions)" checkbox under tax classification requires additional clarification text since this description varies (e g , "foreign partnership" or "non profit"), the value can be passed at runtime using a runtime variable and then mapped into the pdf field steps to create a runtime variable create the variable go to the resources tab → + create new resource and select runtime variable give it a label (e g , other clarification ) set value type as string insert the variable click + insert text , then click the other (see instructions) text field on the pdf in the field api name dropdown, select the runtime variable (e g , other clarification ) click apply to pass a value for this runtime variable, you need to configure how it will be provided during document generation this is typically done in a salesforce flow by creating an apex defined variable , where the value can be assigned based on user input refer to runtime variable docid\ axlc5bzdbbimwpkbabj9 for detailed instructions on configuring and mapping runtime variables in flow 5 publish after mapping click the publish tab in the right panel publish it as a new template (or overwrite an existing one) the template will now be available for generating w 9s from account records and will appear under the document generation → docgen templates tab test the setup once the template is published , it becomes available for use in two ways 1 manual usage from widget the published template will appear in the document generation widget on the related salesforce record (e g , account record) steps to generate manually open the related record (e g , account john doe consulting ) in the cloudfiles docgen widget , locate your published template in the list click generate pdf to create the document once generated, you can download the document copy to salesforce files for attaching directly to the record this method is useful for one off requests , such as when a user needs a form generated immediately 2 automatic usage with flows every template in cloudfiles has a unique template id this id can be used in salesforce flow to trigger document generation automatically using doc generation flow actions docid\ nv27oftdk5ukedfzqpbpq for example when a new account record is created, a w 9 can be generated automatically when a record is updated (e g , stage changes > “onboarded” or “contract signed”), the related pdf template can be generated in the background refer to generate a document via automation docid\ f n6ixvldco8bquh26g8f for automatic document generation with record update