logo

NJP

Transform Attachments with Flow Designer

Import · Apr 30, 2021 · article

On the community I found a lot of questions and blogs on how-to automatically transform attachments from an email into data in a table. These blogs either use script includes, scheduled scripts or business rules and mostly a combination of all. With the Flow Designer this is made a lot easier.

First we need to create a Data Source (Filter Navigator: System Import - Data Source). This record contains all the information about our attachment. In our example we will be creating users with a CSV file (comma delimiter). Save the record.

In the related list - transform, create a new transform map. Provide the mandatory fields (source table is the import set table just created in the data source and target table is the sys_user table). The related link Mapping Assists helps with the mapping of the fields.

Now we can start building our Flow (Process Automation - Flow Designer). Our trigger is an Inbound email with an attachment. The final flow will look something like this.

image

To automate the transform from the attachment on the data source. Action number 4 is created (Click on the + in the tab and create new action).

Create two action inputs:

image

Add a script step (green +). Create two Input Variables and use the pills from the input as values in the variables.

image

The script will look like this:

(function execute(inputs, outputs) {

var dataSource = new GlideRecord("sys_data_source");
dataSource.get(inputs.dataSource);

var loader = new GlideImportSetLoader();
var importSetRec = loader.getImportSetGr(dataSource);
var ranload = loader.loadImportSetTable(importSetRec, dataSource);
importSetRec.state = "loaded";
importSetRec.update();

var transformWorker = new GlideImportSetTransformerWorker(importSetRec.sys_id, inputs.transformMapSysID);
transformWorker.setBackground(true);
transformWorker.start(); 

})(inputs, outputs);

Save and Publish your action. (You can also test if the action works by running a test and providing the inputs with the sysid's from the create transform map and data source.

Back in the flow you can drag the pills to the action this will provide the script with the correct sysids (and makes it reusable):

image

After this you should add the D elete Attachment action to clean up your data source.

Save and activate and you have an automated email attachment transformer.

I am always learning new stuff, so please if you have questions, remarks or ideas on how to do this more easily. Please comment.

View original source

https://www.servicenow.com/community/now-platform-articles/transform-attachments-with-flow-designer/ta-p/2328380