I have an action node that runs function where I merge targeted fields in a docx document with existing data. I am able to merge the document fields and create a new docx document with populated fields, as well as download it. However, I want to store the merged document as a file after it has been merged, in stead of having to download it. I’ve seen a post (How do I send a file object from a coded component to the datastore? - #4 by kristian) where the op had a somewhat similar question, but unfortunately I’m unable to recreate the success he had. Is there a way to store a document as an object that has been created in a function in Appfarm?
Hi!
I believe you should be able to do so. Is it a viable option to copy that function and place it on the “Custom File Content” setting (see screenshot) with correct file name (myfile.docx
etc), and mime type
(application/vnd.openxmlformats-officedocument.wordprocessingml.document
for docx)?
Thanks for looking into it.
Unfortunately I that’s already been tested:
Here is the main part (excluding variable/field assignments) in the code.
async function generateWordDoc(fields) {
const options = {
delimiters: {
start: "{{",
end: "}}",
paragraphLoop: true,
linebreaks: true,
},
nullGetter: () => {
return "";
},
};
try {
// Generating merged Word document - object inn context
const arrayBuffer = await mailMergeFilesTemp.__file.arrayBuffer();
const zip = new PizZip(arrayBuffer);
const doc = new docxtemplater(zip, options);
doc.render(fields);
const docxBlob = new Blob([doc.getZip().generate({ type: "arraybuffer" })], {
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
});
return docxBlob;
} catch (error) {
console.error('Error in document processing:', error);
throw error;
}
const fields = {....
// Rreturn the document blob directly
return generateWordDoc(fields);
I’ve tried to both binding the mime type and setting value, but I’m stuck with the error:
Create File Object - test
Action Node failed: Create File Object - test
Error: Unsupported content for custom file
Catch Exception
Catching Exception
Error: Unsupported content for custom file
Hi!
I am no coder, but running an async functions inside the function editor sounds incorrect the way you do it currently.
If you must use an async function: You may do that in a “Run code” action node (see docs). Since we do not have a “blob” datatype available, save that file-blob temporarily in the browse sessions/window variable (e.g. inside the run code, have something like window.myfileblob = generateWordDoc(fields)
. And then, in the Create File Object, return that window.myfileblob using a function in the “Custom File Content” setting.