The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Editing CSV attachment before loading to data source

RyanPSchneider
Tera Expert

We have an emailed CSV attachment that we want to load and map to our environment but the first three /r/n prevent us from being able to load the data in.

 

Has anyone made a custom flow designer action or script that can remove the first X lines from a CSV attachment inbound?

1 ACCEPTED SOLUTION

nityabans27
Giga Guru

Hi @RyanPSchneider,

 

Approach: Flow Designer Custom Action to Strip First X Lines

Inputs:

  • attachmentSysId → sys_id of the CSV attachment

  • linesToSkip → number of lines to remove (e.g., 3)

Outputs:

  • cleanedCSV → CSV string with header/junk lines removed

Server-side Script (example):

(function execute(inputs, outputs) {
var gsa = new GlideSysAttachment();
var bytes = gsa.getBytes(inputs.attachmentSysId);
var content = Packages.java.lang.String(bytes, "UTF-8"); // convert to string

// Split into lines, handle both \r\n and \n
var lines = content.split(/\r?\n/);

// Remove first X lines
var cleaned = lines.slice(parseInt(inputs.linesToSkip, 10)).join("\n");

// Return cleaned CSV as output
outputs.cleanedCSV = cleaned;
})(inputs, outputs);

 

🔄 How to Use in a Flow

  1. Flow trigger → e.g. Inbound Email with attachment.

  2. Custom Action (above) → input = attachment sys_id, linesToSkip = 3.

  3. Use cleanedCSV output in your next step:

    • Write back to a new attachment, or

    • Parse directly with your CSV parser/import logic.

View solution in original post

4 REPLIES 4

palanikumar
Giga Sage

Hello @RyanPSchneider ,

Below code will help you to read the content of attachment. Use this code to read attachment and remove any unwanted lines, delete the current attachment and add new attachment with the updated value to the record:

var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', current.sys_id);
att.orderByDesc('sys_created_on');
att.query();
while (att.next()) {
    var attach = new GlideSysAttachment().getContentStream(att.sys_id.toString());
    var reader = new GlideTextReader(attach);
    var ln = ' ';
    while ((ln = reader.readLine()) != null) {
        gs.info(ln);
    }
}

 

Thank you,
Palani

nityabans27
Giga Guru

Hi @RyanPSchneider,

 

Approach: Flow Designer Custom Action to Strip First X Lines

Inputs:

  • attachmentSysId → sys_id of the CSV attachment

  • linesToSkip → number of lines to remove (e.g., 3)

Outputs:

  • cleanedCSV → CSV string with header/junk lines removed

Server-side Script (example):

(function execute(inputs, outputs) {
var gsa = new GlideSysAttachment();
var bytes = gsa.getBytes(inputs.attachmentSysId);
var content = Packages.java.lang.String(bytes, "UTF-8"); // convert to string

// Split into lines, handle both \r\n and \n
var lines = content.split(/\r?\n/);

// Remove first X lines
var cleaned = lines.slice(parseInt(inputs.linesToSkip, 10)).join("\n");

// Return cleaned CSV as output
outputs.cleanedCSV = cleaned;
})(inputs, outputs);

 

🔄 How to Use in a Flow

  1. Flow trigger → e.g. Inbound Email with attachment.

  2. Custom Action (above) → input = attachment sys_id, linesToSkip = 3.

  3. Use cleanedCSV output in your next step:

    • Write back to a new attachment, or

    • Parse directly with your CSV parser/import logic.

hi @RyanPSchneider 

 

Starting with the answer provided by @nityabans27 

I wrote some code which worked perfectly for writing a new attachment to the sys_attachment table.

 

I created a new Script Action in flow designer. 

 

I have written the steps and code in the attached word document.

 

Do let me know if it helps.

 

Thank you 

Anu

 

 

Thank you so much! I had to do some small tweaks to get this to work but this did end up working!

First was that I had initially inputted the string of the attachmentSysID vs. the object reference of the attachment. The object reference works with the getBytes. This was probably on me to realize earlier so I struggled with that one for a bit.

 

For the code, here is what I ended up doing down below

 

For the Line 7 - I didn't need the full regex for carriage return/line break. I just needed \n for it to work.

For line 10,  I removed the parse int function for line var cleaned and just set an input integer to use.

I then wanted to write the attachment directly to the data source so used .write() to use set inputs for my data record, file name and content type.

 

Lastly to test, I output the different variables for content, lines and cleaned, I set them to outputs so I didn't have to write to a log and search that way. It helped confirm faster when I tested.

 

Hopefully this helps someone else as it did for me!

 

(function execute(inputs, outputs) {
var gsa = new GlideSysAttachment();
var bytes = gsa.getBytes(inputs.attachmentSysId);
var content = Packages.java.lang.String(bytes, "UTF-8"); // convert to string

// Split into lines via \n
var lines = content.split("\n");

// Remove first X lines
var cleaned = lines.slice(inputs.linesToSkip).join("\n");

gsa.write(inputs.datasourcesys, inputs.active_custodians_trim, inputs.contentType, cleaned);

outputs.content = content;
outputs.cleancsv = cleaned;

})(inputs, outputs);