If you are performing requests to generate documents from Salesforce you could run into scenarios where merge files are not being stored in the Salesforce records, and find Apex Exceptions in the logs like the below:
What is the limitation?
Salesforce allows API connections to stay open for up to 120 seconds. If processing a document takes longer than those two minutes, Salesforce closes the connection before the document can be generated for it to be sent back.
What affects processing time?
Processing time has many factors. The document size, amount of data processed, DocX to PDF conversion, server load times, etc.
How do I work around this?
You can workaround this time limitation by using Salesforce delivery on the document to guarantee that the document will always be returned to the Salesforce record it was sent from.
The Salesforce Delivery method moves documents sent back to Salesforce outside of the API and away from that 120-second limit.
Step 1: Set up a Salesforce delivery on the document using this article as needed.
Enter {$RecordID) in the "Merge Field" area and enter the Salesforce Object API Name you would like to use.
Step 2: From the Formstack Mapping in Salesforce, uncheck "Save Documents as Attachments."
Note: You can leave the "Preview" and "Immediate Download" checked, however, these both rely on the API as well.
Step 3: From the Formstack Mapping in Salesforce, scroll to the bottom to see RecordID. Map the RecordId to the Id(e.g Account Id) of the object you are merging from in order for the delivery to know where to deliver the document.
Alternatively, to avoid timing out an API connection, convert your Docx template into Document Builder or Fillable PDF or change your document output to Docx to avoid the conversion process.