- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎07-26-2022 12:23 PM
I had a requirement to generate an attachment and I wanted to initiate this via Flow Designer. Unfortunately there is no out of box (OOB) action as part of the 'ServiceNow Core' spoke to perform this. This article is a guide on how to create such an action.
You can see the existing ServiceNow Core Attachments actions below:
The steps below will outline how to create a custom action that generates an attachment.
Step 1 - Create a new Action:
After opening Flow Designer (via filter navigator under the Process Automation application) click on the 'Action' button under 'New':
Step 2 - Configure the Action properties:
Provide the Action Name, set the Category as Attachments and add a suitable description:
Step 3 - Configure the Action Inputs:
We require five mandatory inputs.
- 'Table Name': defines the table where the record resides that the attachment will be added.
- 'Record sys_id': defines the sys_id of the record the attachment will be added.
- 'File Name': defines the attachment file name
- 'Content Type': defines the attachment content type (A Choice Type; examples include text/plain, text/csv, application/pdf)
- 'Content': - defines the attachment content
Step 4 - Configure the Script Step:
Create five Input Variables for the Script Step. They will consume the Action Input variables available in the Data pane:
Add the actual script that will generate the attachment on the record we specify via our inputs and create an Output Variable that will capture the attachment sys_id:
The script itself is utilising the GlideSysAttachment API (documentation located here on the developer docs). Essentially it gets the record from the table (that we provided as an input) and writes the attachment to that record using the other inputs; file name, content type and content:
(function execute(inputs, outputs) {
var attach = new GlideSysAttachment();
//set up inputs
var rec = new GlideRecord(inputs.tableName);
rec.get(inputs.recordSysID);
var fileName = inputs.fileName;
var contentType = inputs.contentType;
var content = inputs.content;
var agr = attach.write(rec, fileName, contentType, content);
outputs.attachment = agr;
})(inputs, outputs);
Step 5: Configure the Action Output:
By creating an action output we can then pass back the sys_id of the generated attachment record to be consumed by a flow that will utilise the action. (Note: this sys_id will reference the attachment in the sys_attachment table):
Step 6: Test, Save and Publish Action:
The action can now be tested providing the required inputs. Once verified that the attachment is created as expected, save and publish the action. The action can then be located in the 'Global' spoke and used in a flow as required:
That's everything completed! A custom action to generate an Attachment on a record of your choice.
Use Case Example:
Below I demonstrate a sample use case I've mocked up. A flow triggered on creation of a Catalog Task makes an API call with the Test API action. The response data makes up the content of the attachment that is added to the trigger record.
In the sample flow below, highlighted is the API Data pill (output from the Test API action). Pass that into the Content input and complete the other mandatory inputs:
The execution details of the flow below highlight the inputs and successful Attachment sys_id output:
Finally, on the Catalog Task the attachment is added as expected:
Credit to vg47641 and the top rated answer on this community question for inspiring some further research on the developer docs here to fine tune this solution.
I hope you found this article useful. Feel free to bookmark or add any comments below.
Geoff
- 13,047 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This is helpful, thank you.
Something that's bugging me, however, is that I can't find a list of content types for creating attachments.
I've seen text/plain, text/csv, application/csv, and application/pdf in use so far, but I can't find a comprehensive list of available types. Any chance you have info on that?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Please check if this helps.
docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
doc: application/msword
jpg: image/jpeg
png: image/png
xlsx: application/xlsx OR application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xls: application/vnd.ms-excel
pptx: application/vnd.openxmlformats-officedocument.presentationml.presentation
pdf: application/pdf

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Nice article, but you should add some error handling in the script, to make it more robust.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
when saving the action it does not let me get the following error marked in green
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
when saving the action it does not let me get the following error marked in green
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Greoff,
I have created a custom action to add the attachment, Can we add the attachment in the Email Body in the "Send Email" action in flow designer.
Can you please guide me how to achieve it.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Geoff_T - Thank you for this custom solution, it's great.
The only issue I am getting is with the PDF file type when trying to access/open the PDF document from the record it always gives an error as attached. Have checked in different machines and it is always the same.
Is it something within the script step that needs to be modified?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Suryansh Verma @Geoff_T - did you ever find a solution for the error message? I have a similar error message when I save as a PDF.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Shane Bjork : I wanted to attach the inbound email as a PDF attachment to the record but did not work, so I am now using .eml as the solution for now in my implementation.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Suryansh Verma - Thanks, I can get plain text and word to open just fine. it is the PDF generations that does not appear to be working for me either.
@Geoff_T I like the solution, do you have a path that works for PDF as well?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Adding this for anyone that is trying to attach a PDF servicenow flow designer generate pdf document - Google Search
I'm not the video creator so make sure you give him some love, but basically if you're just using the above script, you're not actually generating anything just a string, which is why you can't open the PDF file because it's not actually a PDF.
We can use the OOTB sn_pdfgeneratorutils.PDFGenerationAPI; in the script to generate our content and format it as need, documentation here: PDFGenerationAPI - Scoped, Global | ServiceNow Developers
Hope this helps 🙂