[ServiceNow Flow] Read email attachment file and update the ServiceNow table

Kishore32
Tera Contributor

Hello,

I've a requirement to read the inbound email with attachment in flow and I'll get list of users in attachment and read the attachment line by line and activate the user by checking active field in user table for the user record.

This should happen completely through flow.

Is there any action that handles this situation? Do let me know if you need more info.

Thanks,

Kishore.

1 ACCEPTED SOLUTION

Nitin22
Tera Expert

yes , you will have create flow with inbound email trigger, then create script action to get attachment table. After that you can read attachment by converting data in bytes

Sample Script

var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', recordID);
attachment.query();
if ( attachment.next() ) {
var attachID = attachment.sys_id;

var attach_data = new GlideSysAttachment();
var by_data = attach_data.getBytes(attachment);
read_data = Packages.java.lang.String(by_data);
}

After that you need to process read_data as per your requirement.

 

If you find this answer helpful please don't forget to mark it Helpful. Thank you.

View solution in original post

6 REPLIES 6

Nitin22
Tera Expert

yes , you will have create flow with inbound email trigger, then create script action to get attachment table. After that you can read attachment by converting data in bytes

Sample Script

var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', recordID);
attachment.query();
if ( attachment.next() ) {
var attachID = attachment.sys_id;

var attach_data = new GlideSysAttachment();
var by_data = attach_data.getBytes(attachment);
read_data = Packages.java.lang.String(by_data);
}

After that you need to process read_data as per your requirement.

 

If you find this answer helpful please don't forget to mark it Helpful. Thank you.

How can we loop through each line from the variable?

In this case 'read_data' variable having data, can we go through line by line?

If we can loop through then I wanted to add each line value to an array and the array will be used in next step of flow to update a record.

Thanks

it depends on your attachment type whether it is doc , excel,csv etc. You can print read_data variable on basis of rendered data you can split. For example for CSV you can use line break to seperate all line then use a for loop and you can read each line individually and  process data further using "," as a delimiter. after that you can pass required data by creating an array in output variable of script action. Then you can map that script output with action output which can be used in next step of flow.

Can you tell me what's the issue with below script

(function execute(inputs, outputs) {
var counter = 0;
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.get('table_sys_id', inputs.recId);
var ga = new GlideSysAttachment();
var base64EncodedData = GlideBase64.encode(ga.getBytes(grAttachment));
var fileData = GlideStringUtil.base64Decode(base64EncodedData);
var fLines = fileData.split(",");
outputs.content = fLines;
outputs.counter = fLines.length;
//foreach(var line in fileLines){
for (var i = 1; i < fLines.length; i++){
outputs.filelines.push(fLines[i]);
}

})(inputs, outputs);

 

CSV:

find_real_file.png

Do let me know if you need any other information