Use Salesforce “Mail Merge Templates” to update Word Document Fields
When its comes to update word document using salesforce data, there is three known solutions.
- Use visualforce to generate documents using HTML coding and fill relevant data fields using apex & javascript.
- Use mail merge templates to update the template of the original document.
- Use third-party tools (DocuSign)
Of course, each solution has own pro & cons. such as when we use visualforce we cannot 100% guarantee that we can generate document same as original(document styles), but we can add data whatever we want and however we want( like tables). In mail merge data that we can add to the doc is limited. But on the other hand, because we are updating the original document it will be the same as the original.
Now, let's focus on the Mail merge template option.
First, we need to add fields that we need to add to the document. Let's say we are generating a letter to send to customers, we need to update templates with contact record name and address fields.
we can get field names that need to add to the template by this.
https://your-org-domain-name/servlet/servlet.SForceMailMerge?id=recordId
replace your domain name and “recordId” with your vales.
Select a field name that you want to add to the document.
Then go to the document, click on the “Insert” section and click on the “Quick Parts” option, and select “field..”.
Then on popup box search and select field name called “MergeField” add the field name and click ok.
Now document should look like this.
Now go to the setup on your org and search “Mail Merge Templates” on the quick find box.
And click on New templates.
Upload template and fill the required fields and save it.
Now go to the contact object, object manager page, and create a “Detail Page URL Button”. Use the below “URL” to open single Mail Merge page on a new tab.
On this URL,
“id” = Id value of the record that needs to generate fields values.
“p3” = Id value of contact that needs to send the document via email.
“p8” = Id value of Mail merge
You can get templated Id when you select template name from the list from the URL ( ex - template/lightning/setup/CommunicationTemplatesWord/page?address=%2F01H3M00000004wA).
When you click on the button you will direct to single Mail merge page and you will need to click on the “Generate” button to send the document. The field will automatically be filled by salesforce when the document is generated. we don't need to send values via URL.