logo

NJP

Generate an Attachment via custom Flow Designer Action

Import · Jul 26, 2022 · article

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:

image

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':

image

Step 2 - Configure the Action properties:

Provide the Action Name, set the Category as Attachments and add a suitable description:

image

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

image

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:

image

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:

image

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):

image

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:

image

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:

image

The execution details of the flow below highlight the inputs and successful Attachment sys_id output:

image

Finally, on the Catalog Task the attachment is added as expected:

image

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

View original source

https://www.servicenow.com/community/developer-articles/generate-an-attachment-via-custom-flow-designer-action/ta-p/2306822