Workflow: Create file and attach it to a resource

Hi,

I am using the 2021.9 version and I have an issue with using the workflow step “Create file…”. I would like to generate a PDF file with a summary for my customer and allow my user to download it. I have gathered the data and used the “Render template” function. As the result of this function, I am getting the report (RenderedDocument) variable. As a next step, I am trying to use the “Create file…” function. Those are my input parameters:

  • content* (Reader) report.document (output variable from the “Render template”)
  • resource* (ComposeRecord) record (the workflow will be started on the record details page)

I am also defining a name for the output variable (attachment).

I am getting an error: “not allowed to create empty attachments”

  1. I have no idea why this error occurs (I have used prompted to verify the report.document variable is not empty)
  2. I am unable to locate any documentation regarding using the Attachment:
  • how to add it to the record?
  • do I need any special field within the record where any link to the attachment is held?
  • how do I access the attachment?

Any help will be as always appreciated :slight_smile:

I messed up the attachment handling code so we’ll need to do a workaround for now; apologies for this.

This is what you need to do (you can click on images to expand them; they’ll be a bit small in preview)

Render the document

This stays as is; just make sure that the parameters are ok; this is what I used:

Prepare a record

Use the compose record maker function to prepare a record (you can use record lookup or other functions that give access to a record).

Make sure that the record has a field of type File upload.

Create an attachment

The raw file needs to be converted to an attachment – Create a file and attach it to a resource function.

Here is where the issue lies – you can’t use the reader (internals expect a ReadSeeker, but the workflow passes in a Reader).

Instead of Reader select the String type. The result will be the same but it does need to do some extra pointless steps under the hood (the handler converts the provided Reader to a String and then back to a ReadSeeker.
There shouldn’t be much of a performance impact unless you’re working with giant documents.

Important make sure to provide the file extension with the file name – welcome_message.pdf from my example.

Reference the attachment to the record

Use an expression step to link the attachment to the record; this is how it is for me (my field is called document)

Screenshot from 2022-04-12 15-02-19

Create/update record

Use a compose record create function when creating new records, or compose record update function when updating existing records.


With this, my documents are being created.

1 Like

As always: thanks a lot. Works great :slight_smile:

1 Like

Perfect :ok_hand:
We also have a patch in place for the issue which should be available ~next week.

1 Like

Is this still the method for creating PDF files and attaching them to a record? I followed this pattern but keep getting an “unknown field” error for my file attachment field name. I’m running 2023.9.4. Any help appreciated.

Still is yes.
For what step do you get this error?

The Create File and Attach to a Resource step (21 in my workflow, attached).

Here’s the mouseover of the previous step where the document object (doc) is defined

1 Like

Use doc.document over doc.cre... for the content argument

OK, that got me past step 21, but the next step crashed with this error:

image

Here’s the step configuration:

1 Like

try attachment.ID here

OK. That solved that problem. One little thing…

Last question on this topic (hopefully): I would like to delete the attachment before generating a new one. For the attachment (ID) argument in the Delete Attachment function, do I just use the field name (in this case, record.values.credserviceTnsg22File), and are there any other steps I need to take? If possible, I don’t want to leave an orphaned file on the server.

1 Like

You just need to pass in the ID; before calling the delete function, do check if the ID exists or not